Java 如何在每次单击libgdx中的按钮时前后移动精灵?
如何使我的玩家在自上而下的游戏中前后移动。我创建了两个按钮Java 如何在每次单击libgdx中的按钮时前后移动精灵?,java,android,libgdx,acceleration,Java,Android,Libgdx,Acceleration,如何使我的玩家在自上而下的游戏中前后移动。我创建了两个按钮moveForward按钮和moveBackward按钮。使用acceleromter我将玩家左右移动。我的主要问题是每次我点击按钮时,我的播放器都会前后移动。它使用上下键工作,但我不知道如何使用按钮触摸来实现 下面是我的代码 // Load the sprite sheet as a texture cat = new Texture(Gdx.files.internal("spriteCatsheet.png"));
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;
}
spriteBatch.draw(当前帧,player.x,player.y);
//键盘上
如果(Gdx.input.isKeyPressed(input.Keys.DOWN))player.y-=300*Gdx.graphics.getDeltaTime();
如果(Gdx.input.isKeyPressed(input.Keys.UP))player.y+=300*Gdx.graphics.getDeltaTime();
如果(Gdx.input.isKeyPressed(input.Keys.LEFT))player.x-=300*Gdx.graphics.getDeltaTime();
如果(Gdx.input.isKeyPressed(input.Keys.RIGHT))player.x+=300*Gdx.graphics.getDeltaTime();
//移动加速度
if(Gdx.input.isPeripheralaAvailable(input.Peripheral.Accelerator)){
player.x-=Gdx.input.getAccelerometerX();
player.y+=Gdx.input.getAccelerometry()/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;
}
Thank’s and Advance ^ ^按下按钮时,除了输出一些控制台文本外,您什么也没做。您应该在侦听器中调用一个方法来移动播放器
除此之外,触地是“刚触碰”,触地是“释放”。而不是相反。在您的情况下,您希望具有“按钮保持”功能,因此您需要为每个按钮设置一个标志,这些按钮在触地/刚按下时被激活,在触地/释放时被禁用。当标志处于激活状态时,这意味着按下按钮。然后,在更新循环中,检查按钮/标志X是否被按下,然后施展您的魔法。您可以这样使用:
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的侦听器方法中
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;
}
});
执行“向前移动”按钮。按下按钮时,除了输出一些控制台文本外,您什么也不做。你应该在监听器中调用一个方法来移动播放器。您好,先生@Madmenyo我删除监听器中的方法我删除这个player.y+=300*Gdx.graphics.getDeltaTime()
它向前移动,但我认为这不是向前移动播放器的正确方式,因为它正在抓拍。getDeltaTime
是平均增量时间(我认为是最后100帧)。如果使用getRawDeltaTime
,它可能不会那么快。如果它真的(非常明显)急躁/急躁,那一定是其他原因。我已经改成getRawDeltaTime
了,但仍然很急躁/急躁。。