请帮帮我。我希望当按下屏幕左侧时模型向左移动,当按下屏幕右侧时,模型向右移动。
InputHandler类
public class InputHandler implements InputProcessor {
private Bird myBird;
//
public InputHandler(Bird bird) {
// myBird and bird
myBird = bird;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
myBird.onClick();
return true;
}
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
鸟类
public class Bird {
private Vector2 position;
private Vector2 velocity;
private Vector2 acceleration;
private float rotation; // For handling bird rotation
private int width;
private int height;
public Bird(float x, float y, int width, int height) {
this.width = width;
this.height = height;
position = new Vector2(230, 650);
velocity = new Vector2(0, 0);
acceleration = new Vector2(200, 0);
}
public void update(float delta) {
velocity.add(acceleration.cpy().scl(delta));
if (velocity.x > 200) {
velocity.x = 200;
}
position.add(velocity.cpy().scl(delta));
// Turn
/*if (velocity.y < 0) {
rotation -= 600 * delta;
if (rotation < -20) {
rotation = -20;
}
}*/
/* Turn
if (isFalling()) {
rotation += 480 * delta;
if (rotation > 90) {
rotation = 90;
}
}*/
}
/*public boolean isFalling() {
return velocity.y > 110;
}*/
public void onClick() {
velocity.x = -140;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public float getWidth() {
return width;
}
public float getHeight() {
return height;
}
public float getRotation() {
return rotation;
}
答案 0 :(得分:1)
将touchDown代码更改为此并删除评论标记
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if (screenX < WIDTH_OF_SCREEN / 2) {
myBird.setLeftMove(true);
} else {
myBird.setRightMove(true);
}
return true;
}
然后将您的touchUp代码更改为此
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
myBird.setLeftMove(false);
myBird.setRightMove(false);
return true;
}
您需要将WIDTH_OF_SCREEN替换为屏幕宽度。这将做的是它将检查屏幕被按下的位置,左侧是不到屏幕的一半,右侧是屏幕的一半以上。当手指抬起时,它将停止向左和向右移动。