lewster32 / phaser-plugin-isometric

Feature-packed axonometric plugin for Phaser 2 which stays true to the Phaser API.
http://rotates.org/phaser/iso
MIT License
472 stars 165 forks source link

Manually updating sprite's position #24

Closed larsonjj closed 8 years ago

larsonjj commented 8 years ago

Hello,

I'm running into an issue where I need to update a sprite's isoPosition at a specific time interval. Basically I'm communicating a sprites position to a Node server from different browser clients and verifying those positions via websocket at a consistent time interval.

For example, I can replicate the issue i'm seeing if I add the following code snippet after line 80 for the example on: http://rotates.org/phaser/iso/examples/character.htm

setInterval(function() {player.isoX = 128; player.isoY = 128}, 1000);

Once you move the cube sprite using the keyboard, you will see the sprite start to flicker and constantly change positions in a seemingly random fashion.

It's super weird and I can't figure out how to resolve it.

Maybe their is a better way to update a sprite's position?

Awesome plugin BTW :+1:

larsonjj commented 8 years ago

I actually found a solution based on a post you made @lewster32 :+1:

Basically, you need to set player.body.moves = false before making any updates to the player.isoX or player.isoY properties. Then after making the position updates, reset player.body.moves = true.

Hope this helps anyone else who runs into the same issues.

lewster32 commented 8 years ago

Yeah - for reference, what's happening is the physics system when enabled tries to take control of the position of an object. If you manually change the position while the physics system is in control, all kinds of weirdness will happen, such as juddering or the object suddenly flying off in a direction. By setting body.moves = false, you tell the physics system to stop trying to move the object, and thus allow you to manually set the position. Once it's moved, you can re-enable it and the physics system will just resume from its new position.

wassname commented 8 years ago

Sorry for reviving this, and feel free to ignore this if your mind is in other places.

One frequent issue is that sprites that are manually moved during update tend to fly off. This is because sprite.body.prev lags behind, and the difference is applied to the sprites position in postUpdate (as a proxy for velocity).

Possible solutions when manually moving a sprite are:

Thoughts?

lewster32 commented 8 years ago

All valid ways of doing it - I guess it's down to how you want to implement it. I'd usually avoid playing with the internal properties if I can, as they've usually been made internal for a good reason - though saying that on occasion there's no other way to go about it.

wassname commented 8 years ago

Thanks for the sanity check!