ritzbitz01 Posted June 21, 2014 Share Posted June 21, 2014 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 More sharing options...
lewster32 Posted June 21, 2014 Share Posted June 21, 2014 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 More sharing options...
ritzbitz01 Posted June 21, 2014 Author Share Posted June 21, 2014 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 More sharing options...
lewster32 Posted June 21, 2014 Share Posted June 21, 2014 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 More sharing options...
ritzbitz01 Posted June 22, 2014 Author Share Posted June 22, 2014 Turns out this is not an issue in 2.0.4, so must have been introduced in 2.0.5, the version I am using. Will write an issue in github, hopefully it is a quick fix. Thanks for your help! Link to comment Share on other sites More sharing options...
Recommended Posts