Reputation: 35
Please help me. I want that when pressing the left part of the screen the model moved to the left, and when pressing the right part of the screen, model moved to the right.
InputHandler class
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;
}
Bird class
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;
}
Upvotes: 1
Views: 52
Reputation: 520
Change your touchDown code to this and remove the comment tags
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;
}
and then change your touchUp code to this
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
myBird.setLeftMove(false);
myBird.setRightMove(false);
return true;
}
You will need to replace the WIDTH_OF_SCREEN with the width of your screen. What this will do is it will check to see where the screen was pressed, the left being less than half the screen and the right being more than half the screen. When the finger is lifted it will stop moving them to the left and right.
Upvotes: 1