如何让我的玩家在我自上而下的游戏中向前和向后移动。我在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;
}
谢谢和推进^ _ ^
答案 0 :(得分:1)
您可以这样使用:
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按钮。
答案 1 :(得分:0)
除了在按下按钮时输出一些控制台文本,您没有做任何事情。你应该在监听器内调用移动播放器的方法。
除此之外,触地得分是&#34;刚触及&#34;和补救是&#34;释放&#34;。不是相反。在你的情况下,你希望有&#34;按钮保持&#34;功能因此您需要为touchDown /刚刚按下时激活的每个按钮设置一个标志,并在touchUp / release时停用。当标志处于活动状态时,这意味着按下按钮。然后在你的更新循环中,你检查按钮/标志X是否被按下并做你的魔术。