Jump to content

Sprite.addChild(child) passes the child as a copy?


nicoloko02
 Share

Recommended Posts

Hello,

I want to pass a Sprite that will rotate around a pivot sprite. So the child sprite will be added as a child using addChild. I want my bat to follow the childSprite, so I passed it to the follow variable.

Example:

this.pivotSprite = this.create(player.position.x, player.position.y + 100, 'invisible_block');

var childSprite = new Phaser.Sprite(this.game, 0, this.pivotSprite.position.y - 100, 'invisible_block');
this.game.physics.arcade.enableBody(childSprite);
childSprite.pivot = this.pivotSprite.position;
childSprite.body.angularVelocity = 50;

this.pivotSprite.addChild(childSprite);

var bat = new Bat(game, 0, 0);
bat.follow = childSprite;

So, the point is, the childSprite will rotate around a pivotSprite, and my bat will follow that childSprite.

But, the problem is that when I use "Phaser.Physics.Arcade.moveToObject(bat, bat.follow, 100)" the bat goes to the initial position provided in "var childSprite = new Phaser.Sprite(this.game, 0, this.pivotSprite.position.y - 100, 'invisible_block');" and stay there.

I'm using the Phaser.debug.body to check the child, and she is really moving around the pivotSprite.

Is that right? Because I was wondering that childSprite and bat.follow would be the same object.

Edited by nicoloko02
Sorry, I changed the variable names just for better understanding and i forgot some of them o.õ
Link to comment
Share on other sites

Debugging I saw that they were really the same, but I didn't understand why when I was using "Phaser.debug.body" I saw the sprite rotating around the pivotSprite, and when I check for it's position it stayed in position (0, 100).

So, in the update of my Bat class, that inherits from Sprite, I used this code:

var point = this.follow.toGlobal(new Phaser.Point(0, 0));

this.game.physics.arcade.moveToXY(this, point.x, point.y, this.moveSpeed);

And that way worked as I wanted \o/

I looked at documentation, and now I understand why wasn't working before.

The reason is that the Sprite.position is relative to the local coordinates of the parent, so I had to convert it using the Sprite.toGlobal.

I started using Phaser recently and I'm really enjoing it, maybe soon I will have a little game to post here =-D

Thanks for your help Rich.

PS: sorry for my bad english x-D

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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