首页 > 解决方案 > 调用 setPosition 时,精灵如何缓慢移动

问题描述

我是 LibGDX 库的新手,我正在用它构建蛇和梯子游戏。我有一个播放器类,它从 Sprite 类扩展而来。我希望它在掷骰子时移动。但它变得突然,我看不到运动。

public void updatePlayer(){
    //this.setPosition(body.getPosition().x, body.getPosition().y);
    game.getBatch().begin();
    draw(game.getBatch());
    game.getBatch().end();
}

public void updatePlayer(int dice){
    if (dice > 0 && dice <= 6){
        for (int i = 0; i < dice; i++){
            float laterX = getX() + 48;
            if (laterX > GameInfo.WIDTH - 20){
                //setPosition(3, getY() + 5f);
                translateY(5);
                updatePlayer();
               
            }else {
                
                translateX(5);
                //setPosition(getX() + 5, getY());
                updatePlayer();
                
            }
        }
    }
}

标签: javalibgdx

解决方案


你看不到运动,因为你设置了位置。使用 translate 移动它。 文档

Sprite#setPosition() //Sets the position where the sprite will be drawn
Sprite#translate()  //Sets the position relative to the current position where the sprite will be drawn.

我的循环类:

public class Game extends ScreenAdapter {

private static String worldName;
private EntityPlayer player;
private FlatWorld flatWorld;
public static World world;
//    private Box2DDebugRenderer debugRenderer;
private static boolean pauseGame;

public Game(String worldName){
    world = new World(new Vector2(0, 0), false);
    //this.debugRenderer = new Box2DDebugRenderer();
    this.worldName = worldName;
    this.player = PlayerDiskUtils.readPlayerFromDisk(this.worldName);
    this.flatWorld = FlatWorldUtils.loadFlatWorld(this.worldName);
    this.player.setCamera(this.flatWorld.getWorldSize());
    this.flatWorld.setBlocksCourt(FlatWorldUtils.readBlocksCourt(this.flatWorld.getWorldSize().getWidth(), this.flatWorld.getWorldSize().getHeight(), this.worldName));
    Gdx.input.setInputProcessor(new GameInputAdapter());
}

@Override
public void render(float delta) {
    world.step(1/60f, 6, 2);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    Gdx.gl.glClearColor(0f, 0f, 0f, 1f);

    this.flatWorld.getBlocksCourt().renderWorld(player.getCamera());
    this.player.update(this.flatWorld, this.flatWorld.getWorldSize().getWidth(), this.flatWorld.getWorldSize().getHeight());
    this.flatWorld.blocks.renderBlocks(this.player.getCamera());
    this.player.renderInventory();
    this.player.playerHud.render(this.player.states.getHealth(), this.player.states.getHungry(), this.player.states.getThirsty());
}

@Override
public void dispose() {
    this.pause();
    world = null;
    worldName = null;
    this.flatWorld.blocks.disposeBlocks();
}

@Override
public void pause() {
    super.pause();
    PlayerDiskUtils.writePlayerToDisk(this.worldName, this.player);
    FlatWorldUtils.saveFlatWorld(this.worldName, this.flatWorld);
}

public static String getWorldName() { return worldName; }
public static boolean isPauseGame() { return pauseGame; }
public static void setPauseGame(boolean pauseGame) { Game.pauseGame = pauseGame; }


/**
 * This is the input adapter of the player.
 * Here keys and input event are set
 */
private class GameInputAdapter extends InputAdapter{

    @Override   //Mouse moving
    public boolean mouseMoved(int screenX, int screenY) { return player.rotatePlayerToMouse(); }

    @Override   //Keys
    public boolean keyDown(int keycode) {
        if(keycode == Input.Keys.ESCAPE){ if(player.inventoryOpen) player.inventoryOpen = false;}
        if(keycode == Input.Keys.E){ player.inventoryOpen = !player.inventoryOpen; }
        return true;
    }

    @Override   //Mouse buttons
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector3 mouseCords3 = player.getCamera().unproject(new Vector3(screenX, screenY, 0));
        return onBodyClick(new Vector2(mouseCords3.x, mouseCords3.y), button);
    }

    private boolean onBodyClick(Vector2 mouseCords, int button){
        if(player.inventoryOpen) return false;
        final Body[] body = new Body[1];

        //Save inside body[0] the body which has been clicked
        world.QueryAABB(fixture -> {
            if(fixture.testPoint(mouseCords)){
                body[0] = fixture.getBody();
            }
            return true;
        }, mouseCords.x, mouseCords.y, mouseCords.x + 0.5f, mouseCords.y + 0.5f);


        if(body[0] == null){
            if(button == Input.Buttons.RIGHT){
                player.onPlayerRightClick(flatWorld, mouseCords);
            }
        }else{
            if(button == Input.Buttons.RIGHT){
                ((Block)body[0].getUserData()).onBlockClicked(player);
            }else if(button == Input.Buttons.LEFT){
                player.onPlayerBlockDestroying(flatWorld, (Block)body[0].getUserData(), mouseCords);
            }
        }

        return true;
    }
}

}

我关于玩家移动的代码(它是 3d 模型,但它可以帮助你):

     public void move(FlatWorld flatWorld, int worldWidth, int worldHeight) {

    int x = 0, y = 0;
    if (Gdx.input.isKeyPressed(Input.Keys.W))
        y = 1;
    if (Gdx.input.isKeyPressed(Input.Keys.S))
        y = -1;
    if (Gdx.input.isKeyPressed(Input.Keys.A))
        x = -1;
    if (Gdx.input.isKeyPressed(Input.Keys.D))
        x = 1;

    if(x != 0 || y != 0) {
        if (this.canPlayerMove(flatWorld, new Vector3(x, y, 0), worldWidth, worldHeight)) {
            this.modelInstance.transform.translate(x, y, 0);
            this.moveCamera(worldWidth, worldHeight);
        }
    }
    this.renderModel(null);
}

如您所见,我在没有调用 setPosition() 的情况下翻译了我的模型


推荐阅读