Jump to content

Stick a sprite/image to a sprite


ZoomBox
 Share

Recommended Posts

Hello,

 

My problem is simple but I'm working on it for 2 hours at least.

I have a sprite which has a body and a texture.

 

I would like to stick a new image to this sprite without changing anything on it but its appearance.

 

Thank you !

Link to comment
Share on other sites

You can 'stick' any display object to another by doing object1.addChild(object2) - this will add the second object to the first much like a group. Be aware though that the X and Y coordinates of the second object will now be relative to the first object instead of the world (again, like a group) so doing this will make the object suddenly move if it was anywhere other than at 0, 0 originally.

Link to comment
Share on other sites

Hey ! Thank you.

I found this "addChild" function but when I use it, it's like the children are afected by physics.

 

I have a sprite with an arcade body with immovable and allowGravity set on true (basically, a platform in a mario-like). The platform can move horizontally (with a velocity.y applied).

I want to add decoration to this platform, like brushes or anything else, visual only.

 

When I add my decoration, it looks like it has the same behaviour that my platform (so arcade body etc...) but with allowGravity on true. Actually I don't want my decoration to have a body at all.

Should I addChild before I enable arcade body ? If so, is it the only way to do it (because I could want to add decoration later in the game) ?

 

Thank you again for the next answers.

Link to comment
Share on other sites

I found my problem (but it's still odd):

I was enabling the physic body of the parent sprite after adding the children.

 

So doing this:

-Create parent

-Add children

-Enable physics on parent

-Setting specific physics behaviour (immovable=true; for example)

=> will result having a parent and children with physic body BUT only the parent having the specific behaviours defined.

Link to comment
Share on other sites

Enabling a body on a sprite should not enable a body on any of its children, so this behaviour seems a little odd. The usual way this is done is to have only the parent sprite have any physics enabled on it, and all children of that sprite to be controlled manually; this is the cleanest solution given that Arcade physics can only correctly handle separation of two bodies at any one time. I don't even know what would happen if children of a sprite with physics applied were expected to also be controlled by physics... I expect chaos!

Link to comment
Share on other sites

Yep, haha. I suppose it's not the expected behaviour but it's the current one (phaser 2.0.6).

Adding the physic body after the children will result with children that act their own way with their own physic body. THAT IS CHAOS.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...