phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
37.12k stars 7.1k forks source link

Sprite position is wrong at the second creation of a state with Physics.Arcade and collideWorldBounds and camera #1775

Closed vbornand closed 9 years ago

vbornand commented 9 years ago

In one of my game, I have an issue when I restart my state, the position of the player is wrong. I spent a long time to isolate the bug and create a minimal example here: http://phaser.io/sandbox/SUEwRegw

If you press the space key, the camera moves and the circle is on the left of the screen. I you press the "R" key, the state is restarted (with a game.state.restart();) but the circle is not at the same position than on the first start, if you press again the "R" key the circle is at the correct position.

I use a bitmapData to draw a circle, because if I load a picture sometimes the bug doesn't occur. (It depends the picture loading time, with a local image the bug always occurs.)

The issue is in the method Phaser.Physics.Arcade.Body.checkWorldBounds. When the state is reloaded, the x position of the sprite is negative, so the condition (position.x < game.physics.arcade.bounds.x) is true then the position of the sprite is set to 0 (the value of this.game.physics.arcade.bounds.x) and finally the sprite isn't at the expected position.

I didn't find any workaround, and it's a too complex bug for me to fix it.

photonstorm commented 9 years ago

Wow that was a tricky one to track down, but a thankfully very easy fix! The dev branch fixes this - thank you for the really helpful issue report, it was the only thing that allowed me to resolve it!