Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

You know that when the player playing Pokémon games want to change the facing of the main character, one would simply tap on the directional pad (D-pad) on the Game Boy (Color/Advanced/Advanced SP) or Nintendo DS systems, and the character would just turn towards the general direction the player wants to turn to.

As far as I know, the turning mechanics from Pokémon Gold/Silver/Crystal is also combined with the movement mechanics from Pokémon Red/Blue/Yellow, which the latter (movement mechanics) is successfully implemented in Java, shown below.

Movement mechanic:

package entity;

import level.BaseWorld;
import main.Keys;
import resources.Art;
import screen.BaseScreen;

public class TestEntity extends Entity {

    public Keys keys;
    int facing = 0;
    int turning = 0;
    int xPosition;
    int yPosition;

    int xAccel;
    int yAccel;

    boolean lockWalking;
    boolean lockTurning;
    int turningTick = 4;

    public TestEntity(Keys keys) {
        this.keys = keys;
    }

    @Override
    public void initialize(BaseWorld world) {
    }

    @Override
    public void tick() {
        if (!lockTurning) {
            if (keys.up.isDown) {
                turning = 2;
                lockTurning = true;
            }
            else if (keys.down.isDown) {
                turning = 0;
                lockTurning = true;
            }
            else if (keys.left.isDown) {
                turning = 1;
                lockTurning = true;
            }
            else if (keys.right.isDown) {
                turning = 3;
                lockTurning = true;
            }
        }
        if (!lockWalking && turningTick < 0) {
            if (keys.up.isDown) {
                facing = 2;
                yAccel--;
                lockWalking = true;
            }
            else if (keys.down.isDown) {
                facing = 0;
                yAccel++;
                lockWalking = true;
            }
            else if (keys.left.isDown) {
                facing = 1;
                xAccel--;
                lockWalking = true;
            }
            else if (keys.right.isDown) {
                facing = 3;
                xAccel++;
                lockWalking = true;
            }
            if (turningTick < -5)
                lockTurning = false;
        }
        handleMovement();
        turningTick--;
    }

    @Override
    public void render(BaseScreen screen) {
        screen.blit(Art.player[facing][0], xPosition, yPosition, 16, 16);
    }

    //-----------------------------------
    //Private methods

    private void handleMovement() {
        if (lockTurning) {
            facing = turning;
        }
        if (lockWalking) {
            xPosition += xAccel;
            yPosition += yAccel;
        }
        if (xPosition % 16 == 0 && yPosition % 16 == 0) {
            lockWalking = false;
            xAccel = 0;
            yAccel = 0;
        }
    }
}

The movement mechanics I have implemented as of now DOES NOT contain codes that rely on System.currentTimeMillis() or System.nanoTime(), which means it's not elapsed-time-based. Rather, it relies on current-states via fixed game ticks, which is prominent during the Game Boy era.

Unfortunately, it contains a side effect where if the player were to press multiple keys on the D-pad, it will cause the character to turn while it's moving. I can remove only the turning mechanics from the mixed mechanism above, and would obtain the movement mechanics that were used only in Pokémon Red / Blue, Generation 1 games.

However, I would like to use the movement + turning mechanics used from Pokémon Gold/Silver/Crystal, Generation 2 and above.

Could someone suggest an idea / tip / hint / solution for this problem?

You may also use the codes given above to your likings, just make sure to change stuffs a bit while you're at it.

share|improve this question
add comment

closed as too localized by Anko, Byte56, bummzack, msell, Nate May 7 '13 at 22:06

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 1 down vote accepted

Blame me for never having played any of these games, so I can just assume you want your character to turn around while walking rather than turning instantly (e.g. like in The Legend of Zelda games or most JRPGs).

If so, I'd simplify the whole thing using "steering":

  • Store the current direction the player is facing as an integer. You can either use 4 or 8 directions.
  • When tapping a directional key, you set a "target facing"; again as an integer with 4 or 8 possible values.
  • Then, just increase/decrease that value (the current direction) each tick until both match.
  • This way you should get a rather smooth turnaround, that shouldn't mess with actual movement, especially if you only move the character in the current direction.
  • If you're not using pixel graphics/sprite Sheets, you might want to actually use some float/double values for even smoother turning.
share|improve this answer
    
Thank you for your feedback. I've managed to get it to work. But I didn't use the "steering" you've mentioned. Your second point is what helped me. –  tom_mai78101 May 6 '13 at 9:55
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.