Jump to content

game.add.existing issue after moving camera


ritzbitz01
 Share

Recommended Posts

I have created a top down space game where if you hit the cursor buttons (left, right, up, down) it moves the camera.

 

If I call game.add.existing(object) without moving the camera the object gets added to the game as expected.  If I move the camera any amount and then call game.add.existing(object), the object gets added to the game and then immediately moves off the screen.

 

From what I can tell if I move the camera x pixels and then call game.add.existing, every game cycle the newly added object moves x pixels, eventually flying off the screen.

 

Tried going through the phaser code, but couldn't find any reason why this would be happening.

 

Has anyone run into something similar, or know of any way I can fix this?

 

Thanks!

Link to comment
Share on other sites

I think this may a bit of a bug related to the physics code incorrectly calculating velocity based on the fact that the sprite is being re-positioned to account for the camera. It shouldn't really happen if this is the culprit, but in the mean time you may have to do sprite.body.velocity.setTo(0, 0) after adding the sprite (possibly even doing it on a 1 frame timer) or use some other hacky method, and I'd also post this as an issue on Github.

Link to comment
Share on other sites

Do you know where in the phaser code this is happening?  I tried going through the source to track it down and couldn't find anything.  I'd love to have some code to point to when writing an issue.

 

In the meantime I'll try setting the velocity to 0 right after creation.

 

Thanks!

Link to comment
Share on other sites

It's likely something to do with the velocity calculation but I don't want to go sifting through the code right now to find exactly place it's happening. Essentially physics systems often automatically calculate the velocity from movement, so if you have a sprite and you manually move the body 10 pixels to the right, it will assume the body has gained a velocity.x of 10 and continue moving it 10 pixels to the right every subsequent frame. While many checks are put into place to ensure this only happens when you explicitly want it to, Phaser's Arcade physics system seems to have possibly missed a one or two places to do this check - your example being one of them.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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