Sign in to follow this  

Replicating Gameboy Sprite Movement in Java

Recommended Posts

DranoMax    100
Hello everyone,

I've been trying to figure out how one could code so that if the user releases the movement key and the sprite hasn't reached the next "tile" that it will continue to move till it gets there.

The only way I can think to do it is to have an if statement that if the sprite isn't completely in the tile it was moving towards, it will continue moving until it is inside the tile.

I'm sure there's a better way to do this, any suggestions?

Share this post

Link to post
Share on other sites
DonaldHays    278
Try having the move action entirely predetermined from the start, rather than continuously moving as long as a key is held down and handling some special logic if the key gets lifted before movement is complete.

In other words, have logic somewhere in your game loop along the lines of,

"IF sprite is not 'moving' AND keyboard is pressing in a certain direction, THEN begin 'moving' one square in that direction.

IF sprite is 'moving', THEN move it a bit closer to its destination.

IF sprite is 'moving' AND sprite is at its destination, THEN stop 'moving'."

Under such logic, it doesn't matter what the user presses in the time between when the sprite begins moving and when it's finished moving its one square. For all it matters the user could roll their fingers around the arrow keys, and the only direction that will register is what the user is actively pressing when the sprite has arrived at its destination square.

Share this post

Link to post
Share on other sites
Zahlman    1682
No matter what kind of game you are making, you should be separating the physics (the logic that dictates what can move and how) from the rendering (the logic that draws things on the screen). Whatever you display on screen represents the location of objects but does not encode that information. Rendering functions should be "pure" in the sense that they do not modify the game state (i.e. the information that is calculated by physics is read by the rendering code, in order to figure out what to draw, but never modified by that rendering code.)

When the physics restricts sprite movement to the tile grid, it is normal for the physics to encode the sprite location as an integer tile coordinate. Movement between tiles is an animation trick that depends on the previous location, the current location and elapsed time; but in any event the user input is controlling the actual physics position of the object, not the screen position.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this