CLTincknell Posted September 27, 2014 Share Posted September 27, 2014 So I just picked up Phaser and I'm trying to put together an Asteroids clone for practice. I was writing the screen wrap around code and getting some extremely weird behavior, with the ship jittering between the edges and occasionally reversing direction completely. My code is: if(ship.x - ship.width/2 > game.width) {ship.x = -ship.width/2;}else if(ship.x + ship.width/2 < 0) {ship.x = game.width + ship.width/2;}if(ship.y - ship.height/2 > game.height) {ship.y = -ship.height/2;}else if(ship.y + ship.height/2 < 0) {ship.y = game.height + ship.height/2;} and the game is online at https://dl.dropboxusercontent.com/u/93970469/asteroids/index.html I thought the jittering could be caused by the bounds I was using (moving the ship to one side then detecting it was out of bounds and moving it back rapidly) but I'm pretty sure my bounds are good, and this behavior occurs even if I just try to place the ship in the middle of the screen whenever it goes off the edges. In fact doing that caused even more bizarre behavior, with the ship strobing across the screen horizontally. Also, I can't begin to account for why the ship would sometimes reverse directions completely, since this code doesn't touch the velocity components. I've written behavior like this in other games a bunch of times without issue, so I'm at a loss here. Any ideas? Link to comment Share on other sites More sharing options...
lewster32 Posted September 27, 2014 Share Posted September 27, 2014 There is a built-in wrap method on game.world to do it, however that may produce the same results as it does pretty much the same thing you're doing now. The reason the ship is behaving weirdly is due to you wresting control of the position of the object from the physics system - suddenly two different systems are trying to re-position the sprite, your forced x and y positioning, and the physics velocity calculation; the result is mayhem for a brief moment. One way to fix this is rather than setting the sprite position, when it reaches an edge, store the velocity, acceleration and rotation then call sprite.body.reset(x, y) and then re-apply the stored velocity, acceleration and rotation. A better long-term fix is to write something which does this check and move in the sprite's preUpdate, as this happens before the physics system affects the position of the object. This would be more easily implemented by extending Sprite to add this functionality in. Link to comment Share on other sites More sharing options...
CLTincknell Posted September 27, 2014 Author Share Posted September 27, 2014 Cool, thanks for the response. I'll see what I can do. Link to comment Share on other sites More sharing options...
Recommended Posts