tomds

Move a group of sprites together as one body?

Recommended Posts

Hi,

 

I'm trying to overlay a few sprites on top of each other and make them move together as one body, using the P2 physics system. Basically it's meant to be two people sat on a canoe, and they can paddle independently of each other, and so are animated independently of each other. It seems a bit complicated to try and represent all the possible frames of animation with a single sprite, hence why I am wanting to overlay 3 sprites: 1 for the canoe and 2 for the people.

 

The problem I'm having is keeping the people inside the canoe as it moves and rotates! I could give each sprite a separate body and apply the same physics rules to each, but I don't think this will work; really I need to the physics rules to apply only to the canoe, and have the people track the canoe's movement automatically. Is there an easy way to do this? Or am I doing it completely wrong? :)

 

Thanks

Tom

Share this post


Link to post
Share on other sites

You can also put sprites in other sprites, so you can create a 'container' sprite with a body, then put your people inside:

var canoe = game.add.sprite(0, 0, 'canoe');var person1 = game.add.sprite(0, 0, 'person1');var person2 = game.add.sprite(0, 0, 'person2')canoe.addChild(person1);canoe.addChild(person2);game.physics.enable(canoe, Phaser.Physics.P2);

If you need to place the people behind the canoe, then create another top-level sprite and set the key to 'null', then add your people, then the canoe to that.

Share this post


Link to post
Share on other sites

Thanks, you guys are the best! Added the people as children of the canoe sprite and it works - although I had to enable physics on the canoe before adding the people, otherwise they had a starting momentum on game load, and went shooting off to the side!

 

Tom 

Share this post


Link to post
Share on other sites
On 7/10/2014 at 11:05 AM, lewster32 said:

You can also put sprites in other sprites, so you can create a 'container' sprite with a body, then put your people inside:


var canoe = game.add.sprite(0, 0, 'canoe');var person1 = game.add.sprite(0, 0, 'person1');var person2 = game.add.sprite(0, 0, 'person2')canoe.addChild(person1);canoe.addChild(person2);game.physics.enable(canoe, Phaser.Physics.P2);

If you need to place the people behind the canoe, then create another top-level sprite and set the key to 'null', then add your people, then the canoe to that.

I am trying to create a square from bitmapdata make it a sprite and add it to another sprite. But when I add it to another it disappears! Anyone know why this is?

how do you make a square from bitmapdata and then add it as a child to another sprite? What am I missing? Thank you!!

Share this post


Link to post
Share on other sites

If it's disappearing it may be because of its position. Display object positions are relative to their parent. This means that if the square is at 100, 100, and the sprite is also at 100, 100, when you add the square as a child of the sprite, it will be at 200, 200 relative to the screen - i.e. it'll instantly shift 100 pixels to the right, and 100 pixels down.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.