java - 每次在 libgdx 中单击按钮时,如何向前和向后移动 Sprite?

标签 java android libgdx acceleration

如何让我的玩家在自上而下的游戏中前进和后退。我创建了两个按钮 moveForward 按钮和 moveBackward 按钮。使用 acceleromter 我左右移动播放器。我的主要问题是每次单击按钮时我的播放器都会前后移动。它可以使用向上和向下键工作,但我不知道如何使用按钮触摸来实现。

这是我在下面编写的代码

// Load the sprite sheet as a texture
    cat = new Texture(Gdx.files.internal("spriteCatsheet.png"));
    catsprite = new Sprite(cat);
    catsprite.setScale(2f);
    player = new Rectangle();
    player.x = Gdx.graphics.getWidth() - player.width - 350; 
    player.y = catPlayerY;
    player.setWidth(25);

我的按钮

    //left_control
    left_paw = new Texture(Gdx.files.internal("left_paw.png"));
    myTextureRegion = new TextureRegion(left_paw);
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
    moveBackward = new ImageButton(myTexRegionDrawable); //Set the button up
    moveBackward.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("left_paw.png"))));
    //the hover
    moveBackward.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("left_paw_hover.png"))));
    moveBackward.setPosition(10,25);
    stage.addActor(moveBackward); //Add the button to the stage to perform rendering and take input.
    Gdx.input.setInputProcessor(stage);
    moveBackward.addListener(new InputListener(){
        @Override
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("Left Button Pressed");
           //Move player Backward
           //player.y -= 300 * Gdx.graphics.getDeltaTime();
        }
        @Override
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            System.out.print("Released");

            return true;
        }
    });
    stage.addActor(moveBackward);

    //right_control
    right_paw = new Texture(Gdx.files.internal("right_paw.png"));
    myTextureRegion = new TextureRegion(right_paw);
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion);
    moveForward = new ImageButton(myTexRegionDrawable); //Set the button up
    moveForward.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw.png"))));
    //the hover
    moveForward.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("right_paw-hover.png"))));
    moveForward.setPosition(517,25);
    stage.addActor(moveForward); //Add the button to the stage to perform rendering and take input.
    Gdx.input.setInputProcessor(stage);
    moveForward.addListener(new InputListener(){
        @Override
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            System.out.println("Right Button Pressed");
             //Move player Forward
             //player.y += 300 * Gdx.graphics.getDeltaTime();

        }
        @Override
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
            return true;
        }
    });
    stage.addActor(moveForward);

渲染

    spriteBatch.draw(currentFrame,player.x, player.y);

     //On keyboard 
    if(Gdx.input.isKeyPressed(Input.Keys.DOWN))player.y -= 300 * Gdx.graphics.getDeltaTime();
    if(Gdx.input.isKeyPressed(Input.Keys.UP)) player.y += 300 * Gdx.graphics.getDeltaTime();
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) player.x -= 300 * Gdx.graphics.getDeltaTime();
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) player.x  += 300 * Gdx.graphics.getDeltaTime();

    //Mobile acceleration
    if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
        player.x -= Gdx.input.getAccelerometerX();
        player.y += Gdx.input.getAccelerometerY() /1f;
    }
    if (player.x < 0) {
        player.x = 0;
        player.x += Gdx.graphics.getDeltaTime() *20 *delta;
    }
    if (player.x > Gdx.graphics.getWidth()-player.getWidth() -150) {
        player.x = Gdx.graphics.getWidth()-player.getWidth() -150;
    }

谢谢和提前^_^

最佳答案

你可以这样使用:

MotionState motionState=MotionState.NONE;

enum MotionState {

    NONE {
        @Override
        public boolean update(Rectangle player) {
           return true;
        }
    },

    UP {
        @Override
        public boolean update(Rectangle player) {
            player.y += 300 * Gdx.graphics.getDeltaTime();
            return false;
        }
    },

    DOWN{
        @Override
        public boolean update(Rectangle player) {
            player.y -= 300 * Gdx.graphics.getDeltaTime();
            return false;
        }
    },

    LEFT{
        @Override
        public boolean update(Rectangle player)  {
            player.x -= 300 * Gdx.graphics.getDeltaTime();
            return false;
        }
    },

    RIGHT{
        @Override
        public boolean update(Rectangle player) {
            player.x  += 300 * Gdx.graphics.getDeltaTime();
            return false;
        }
    };

    public abstract boolean update(Rectangle player);
}

在您的render() 方法中

if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) motionState = MotionState.DOWN;
if(Gdx.input.isKeyPressed(Input.Keys.UP)) motionState=MotionState.UP;
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) motionState=MotionState.LEFT;
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) motionState=MotionState.RIGHT;

if(motionState.update(player)) motionState=MotionState.NONE;

现在在 Button 的 Listener 方法中

moveBackward.addListener(new InputListener(){
    @Override
    public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
       motionState=MotionState.NONE;
    }
    @Override
    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
         motionState=MotionState.DOWN;  // or what you want 
        return true;
    }
});

为 moveForward 按钮做。

关于java - 每次在 libgdx 中单击按钮时,如何向前和向后移动 Sprite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44150692/

相关文章:

Android加密util类,是否单例

java - 通过TCP将Opencv图像从Android发送到PC

java - LibGDX - 绘制等边三角形

libgdx - 如何在 libgdx 中指定具有色调、饱和度和亮度的颜色

java - GWT、NoSuchElement 异常

android - EditText 不关注具有 NestedScrollView 的 CoordinatorLayout

java - Hashmap的put方法

gradle - 无法将 admob 添加到 libgdx 项目

java - 在 java 中从 cookie 中读取电子邮件字符串

java - JaCoCo覆盖率报告设置(不包括测试类)