Help with tile based movement

Hi guys, I am new here on the Java Gaming forums and could use a little help.

I am programming a game in LWJGL and I need help achieving tiled based moving. I obviously don’t want it just to jump a tile’s width, but as a smooth movement to the next tile (like Pokemon). I tried experimenting with some stuff and here is what I came up with.


if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
	Keyboard.destroy();
	for (int x = 0; x < 32; x += 4) { player.setY(player.getY() - 4); }
	try {
		Keyboard.create();
	} catch (LWJGLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

This kind of works, but the problem is it just jumps the character to the next tile instead of a smooth movement. In theory, this seems like it should work because it moves it 4 pixels 8 times. I am not quite sure what I am doing wrong here.

Also, does Keyboard.destroy() disable the keyboard? I need something to disable movement of the character so you can’t interrupt the movement.

Instead of using a for loop for smooth incrementing of a number try implementing a javax.swing.Timer that runs at a fixed rate.

I have some new code, but this just moves it up by 4 pixels every frame while holding up. What do I need to do to move it up a tile’s length (32 px)?


javax.swing.Timer move = new javax.swing.Timer(17, new ActionListener() {
	public void actionPerformed(ActionEvent arg0) {
		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
			player.setY(player.getY() - 4); 
		}
	}
});

Here lol ~_^:

      player.setY(player.getY() - 4); // change the 4 to 32 

I don’t think you’re quite getting what I am trying to do here. When I press the up key, I want it to slide at 4px a frame up one tile, not press the up key and move up a tile in one frame. Just to explain in a little more, I should press the up key once and the character should move up 1 tile but in a sliding motion and not all in one frame. Hopefully that made sense.

If you already have a game loop up you can use this:


public void tick() {//or whatever your update method is
    if(x % 16 == 0 && y % 16 == 0) {
        motionx = 0;
        motiony = 0;
        if(/*If left key is pressed*/) motionx = -4;
        if(/*If right key is pressed*/) motionx = 4;
        if(/*If up key is pressed*/) motiony = -4;
        if(/*If down key is pressed*/) motiony = 4;
    }
    x += motionx;
    y += motiony;
}

The important thing to remember is that 4 can be any number that goes evenly into 16, otherwise you will go more than one tile.

It kind of worked. Not really though. First off, when I would press a direction, it would move way more a tile’s distance. Then sometimes it would stop but sometimes it wouldn’t. Any clue of what’s going on?

EDIT: Actually, nevermind about the not stopping. The moving too far is still there but I think the problem is that the keyboard needs to be briefly disabled until the character has reached the next tile.

For basic tile movement:
Detect a key press. If true, assign a queue to Player to move into desired point. You can move “jump” or interpolating. What important is ignore anotehr key press until current queue done.

The code I gave you should ignore the keyboard until it is done moving one tile (Oh, and I forgot to mention that the 16 is the tile width / height). I can’t think of why it would be doing that, unless you are moving in a different way. Perhaps I can see your code?

Also, if you want to make sure that they don’t move diagonally, make the ifs a chain of else-ifs.

Here is a paste bin of (almost) all of my code. Most you can ignore because it is incomplete or not used. http://pastebin.com/UBbR07kk

Also ReBirth, you said something about interpolation which I remember now is smooth movement. I remember watching a video on that so I will re watch that.