Jump to content

Moving a Sprite with Multiple Parts


Ryan
 Share

Recommended Posts

EDIT:

 

I have fixed this by making all of the parts the same size and setting the anchor to (0.5, 0).  It's not very elegant, but it seems to be the only way to get sprites of different sizes to stay in position when you flip them.

/** * ADD THE PLAYER TO THE GAME */player_group = this.game.add.group();player_group.x = 200;player_group.y = this.game.world.height - 140;player_foot_back = player_group.create(0, 0, 'player_foot_back');player_foot_back.anchor.setTo(0.5, 0);player_body = player_group.create(0, 0, 'player_body');player_body.anchor.setTo(0.5, 0);player_head = player_group.create(0, 0, 'player_head');player_head.anchor.setTo(0.5, 0);player_foot_front = player_group.create(0, 0, 'player_foot_front');player_foot_front.anchor.setTo(0.5, 0);

-------------------------------------------------

 

Hey guys,

 

I've been playing around with this for a few hours now and I can't seem to get anywhere.

 

Basically, it's a platformer game with a player made up of 4 parts (head, body, feet, hands).  There a things that I need the whole group to do, like gravity.  There are also things I need just parts to do, like the hands will rotate.

/**         * ADD THE PLAYER TO THE GAME         */        player_group = this.game.add.group();                player_group.x = 20;        player_group.y = this.game.world.height - 150;                player_body = player_group.create(18, 35, 'player_body');        player_body.anchor.setTo(0, 0);                player_head = player_group.create(0, 0, 'player_head');        player_head.anchor.setTo(0, 0);                player_feet = player_group.create(25, 122, 'player_foot');        player_feet.anchor.setTo(0, 0);

This positions the parts on the stage just fine.

if (leftKey.isDown) {            player_group.setAll("body.velocity.x", -450);            player_group.setAll("scale.x", -1);        } else if (rightKey.isDown) {            player_group.setAll("body.velocity.x", 450);            player_group.setAll("scale.x", 1);        }

This moves the player fine until it changes direction, then all of the parts are not positioned correctly.  I'm assuming this has something to do with the anchor.

 

Does anyone have any suggestions on the best way to go about controlling a sprite made up of multiple parts?

 

Thanks!

 

FyreTale.

Link to comment
Share on other sites

I am wondering about this too. (So I am adding my question here, I think it boils down to the same one..)

 

If for example I want to give a accessory to my player sprite, that should follow the sprite around (without it's own physics) I thought I could use a group for that.

But since a group does not have a body I can't just have the player be a group..

 

Of course I could set the position of the accessory sprites in every update() function to specific x,y coordinates depending on the player sprite position, but I don't think that that's the correct way.

 

Has anyone any insides into how to move sprites as a unit with the physics engine (ie. velocity)?

Link to comment
Share on other sites

I have done just that.  I have four body parts + the hand/gun.  I've made all the body parts the same width/height and they are part of a group.  I then used group.setAll() to set the gravity etc.  Since the arm/gun will rotate, I couldn't have it be the same size as the other parts, so I had to position it in the update() function.

 

Seems to be the only way around it.

Link to comment
Share on other sites

Sprite.addChild()

Child sprites are positioned relative to the parent, will move with the parent, and can be moved/rotated separately to the parent.

 

 

That sounds like it will do it!

 

+1 vote on adding this to the docs.  I know Rich is a busy man, maybe we could make the docs more like a wiki so the community could build it?

Link to comment
Share on other sites

  • 2 months later...
  • 3 weeks later...
 Share

  • Recently Browsing   0 members

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