简体   繁体   中英

Libgdx: Sprite jitters on movement?

I'm pretty new to the whole of LibGDX and I'm trying to build a top down RPG. I load the map in from a TMX file and render it, now I'm trying to get my guy to just move around with this simple code:

public boolean keyDown(int keycode) {
    switch(keycode){
    case Keys.DPAD_UP:
        velocity.y = speed;
        break;
    case Keys.DPAD_DOWN:
        velocity.y = -speed;
        break;
    case Keys.DPAD_LEFT:
        velocity.x = -speed;
        break;
    case Keys.DPAD_RIGHT:
        velocity.x = speed;
        break;
    }
    return true;
}

which sets the velocity which he'll apply here.

public void update(float delta){
    if(velocity.x > speed){
        velocity.x = speed;
    }else if (velocity.x > speed){
        velocity.x = speed;
    }
    
    if(velocity.y > speed){
        velocity.y = speed;
    }else if (velocity.y > speed){
        velocity.y = speed;
    }
    
    setX(getX() + velocity.x * delta);
    setY(getY() + velocity.y * delta);
}

Now this is all fine and dandy, but as soon as I begin moving it goes wrong. Out of the blue, and completely random, the sprite sometimes just jitters back and the map gets out of place for a split second. This is extremely frustrating and I don't know why this is happening.

This is my player draw code

((BatchTiledMapRenderer) tiledMapRenderer).getBatch().begin();
player.draw(((BatchTiledMapRenderer) tiledMapRenderer).getBatch());
((BatchTiledMapRenderer) tiledMapRenderer).getBatch().end();

Are you using a kind of transparency color and a padding between your tiles in the tileset? That seems not to work sometimes. You could try it without.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM