gpt4 book ai didi

java - libgdx 中纹理的控制

转载 作者:行者123 更新时间:2023-11-30 01:57:26 25 4
gpt4 key购买 nike

请帮帮我。我希望当按下屏幕左侧时,模型向左移动,当按下屏幕右侧时,模型向右移动。

输入处理器类

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;
}

最佳答案

将您的 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 替换为您的屏幕宽度。这将做的是检查屏幕被按下的位置,左边小于屏幕的一半,右边大于屏幕的一半。当手指抬起时,它将停止向左和向右移动。

关于java - libgdx 中纹理的控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32012533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com