nicoloko02 Posted June 9, 2016 Share Posted June 9, 2016 (edited) 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 June 9, 2016 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 More sharing options...
rich Posted June 9, 2016 Share Posted June 9, 2016 Nowhere in the code above is 'point' defined (i.e. "addChild(point)"), so as far as I can see, you're adding a different object as the child. Link to comment Share on other sites More sharing options...
nicoloko02 Posted June 9, 2016 Author Share Posted June 9, 2016 Sorry, I changed the variable names just for better understanding and i forgot some of them o.õ Now it's right. Link to comment Share on other sites More sharing options...
nicoloko02 Posted June 10, 2016 Author Share Posted June 10, 2016 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 More sharing options...
Recommended Posts