I'm using libGDX trying to make a top-down 2D game with movement along the lines of Pokemon. However, I'm not sure how to implement this.
I want the player to be able to press a key (ie: arrow keys or WASD) in order to move, however, when the player releases the key, I don't want the character to stop moving until it's position falls along a grid of cells that consist of 32x32 pixel squares.
So how would I go about creating an 'invisible' grid of 32x32 pixel cells and making my character only stop moving whenever it's on top of one of those cells?
So far I've only done by-pixel movement. Which, I've contemplated I could just have a boolean stating whether the character was moving and just change that if the character landed on the right area, but I haven't thought of any way to implement that.
Here's the method that I'm using for my by-pixel movement (the libGDX covers key input and it draws the sprite based on where the player's bounding box is):
public void generalUpdate() {
if(Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT)) {
if (anim_current == Assets.anim_walkingLeft)
if(player.collides(wall.bounds)) {
player.bounds.x += 1;
}
else
player.dx = -1;
else
anim_current = Assets.anim_walkingLeft;
Assets.current_frame = anim_current.getKeyFrame(stateTime, true);
}
else if (Gdx.input.isKeyPressed(Keys.D) || Gdx.input.isKeyPressed(Keys.RIGHT)) {
if (anim_current == Assets.anim_walkingRight)
if(player.collides(wall.bounds)) {
player.bounds.x -= 1;
}
else
player.dx = 1;
else
anim_current = Assets.anim_walkingRight;
Assets.current_frame = anim_current.getKeyFrame(stateTime, true);
}
else if (Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.UP)) {
if (anim_current == Assets.anim_walkingUp)
if(player.collides(wall.bounds)) {
player.bounds.y += 1;
}
else
player.dy = -1;
else
anim_current = Assets.anim_walkingUp;
Assets.current_frame = anim_current.getKeyFrame(stateTime, true);
}
else if (Gdx.input.isKeyPressed(Keys.S) || Gdx.input.isKeyPressed(Keys.DOWN)) {
if (anim_current == Assets.anim_walkingDown)
if(player.collides(wall.bounds)) {
player.bounds.y -= 1;
}
else
player.dy = 1;
else
anim_current = Assets.anim_walkingDown;
Assets.current_frame = anim_current.getKeyFrame(stateTime, true);
}
else {
Assets.current_frame = anim_current.getKeyFrames()[0];
player.dx = player.dy = 0;
}
player.bounds.x += player.dx;
player.bounds.y += player.dy;
}