标题基本上是什么。
跳跃工作完美,直到我决定通过添加 GameObject 类让我的游戏更加面向对象。每当您点击屏幕时,精灵都会跳转(在jump()中添加一个print语句以验证此方法是否被调用)。谁能发现错误?
我编写跳转的方式:
播放器:
public class Player extends GameObject implements InputProcessor {
private float g;
private float accelerationJump = 9f;
private boolean allowJump = true;
public Player(Sprite spr) {
super(spr);
setxPos(0);
setyPos(getHeight() / 6);
setxSpeed(5.5f);
setySpeed(0);
spr.setX(getxPos());
spr.setY(getyPos());
g = 0.2f;
accelerationX = 0.02f;
}
public void update() {
setySpeed(-g);
//have the sprite constantly moving
moveBy(getxSpeed(), getySpeed());
//if sprite reaches end of screen, move it to the start of the screen again
if (getxPos() > getWidth()) {
setxPos(0);
}
if (getyPos() < getHeight() / 6) {
moveTo(getSprite().getX(), getHeight() / 6);
}
//the player can only jump if he is on the ground. once player is on the ground, he must stop decelerating
if (onGround()) {
allowJump = true;
setySpeed(0);
}
}
public void jump() {
System.out.println("dasdasddadsdasd");
setySpeed(getySpeed() + accelerationJump);
}
// checks whether or not player is on the ground
public boolean onGround() {
return (getSprite().getY() == getHeight() / 6);
}
@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 touchDown(int screenX, int screenY, int pointer, int button) {
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
//if (allowJump && isDragged == false) {
//System.out.println("dsdasdasd");
if(allowJump){
allowJump = false;
jump();
}
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
/*if (isDragged == false) {
isDragged = true;
}*/
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
}
游戏物体 公共抽象类GameObject {
private Sprite spr;
private float xSpeed;
private float ySpeed;
public GameObject(Sprite spr) {
this.spr=spr;
}
public void moveTo(float xPos2, float yPos2) {
setxPos(xPos2);
setyPos(yPos2);
}
public void moveBy(float dx, float dy) {
spr.setY(spr.getY() + dy);
spr.setX(spr.getX() + dx);
}
public float getxPos() {
return spr.getX();
}
public float getyPos() {
return spr.getY();
}
public void setxPos(float xPos2) {
spr.setX(xPos2);
}
public void setyPos(float yPos) {
spr.setY(yPos);
}
public float getxSpeed() {
return xSpeed;
}
public float getySpeed() {
return ySpeed;
}
public void setxSpeed(float xSpeed2) {
xSpeed = xSpeed2;
}
public void setySpeed(float ySpeed2) {
ySpeed = ySpeed2;
}
public int getWidth() {
return Gdx.graphics.getWidth();
}
public int getHeight() {
return Gdx.graphics.getHeight();
}
public void draw(SpriteBatch batch) {
spr.draw(batch);
}
public Sprite getSprite() {
return spr;
}
}
任何帮助都非常感谢。
答案 0 :(得分:0)
您正在将ySpeed设置为-g每次更新()