我创造了3个按钮:'hard','medium'和'easy'。在三种模式中的每一种模式下,速度都会改变,按钮也会起作用。
问题在于我将此行添加到代码中:&& input.getModes() == 'e'
。添加此行后,当我单击任何一个速度按钮时,它会停止。
public class GameWorld {
private final TweenManager manager;
private final CirclePongGame cpgame;
private final MuteButton volumeButton;
private final float buttonSize = 75;
public float gameWidth;
public float gameHeight;
private int velocity = 0;
private Pad pad;
private Ball ball;
private CenterCircle centerCircle;
private int score;
//private char res;
public boolean finish;
private Value secondValue = new Value();
private Value fiveValue = new Value();
private TweenCallback cb;
public ColorManager colorManager;
public InputHandlerMenu input;
private Value distance = new Value();
private ActionResolver actionResolver;
private void collisions() {
{
if (!ball.hasCollided()) {
for (int i = 0; i < pad.getcolCircles().size(); i++)
if (Intersector.overlaps(pad.getcolCircles().get(i), ball.getColCircle())) {
ball.collide();
ball.setCollided(true);
//Gdx.app.log("Angle", ball.getVelocity().toString());
//double perp = 2.0 * ball.getVelocity().cpy().dot(pad.returnNormal(i));
//Vector2 reflectDir = ball.getVelocity().cpy().sub((pad.returnNormal(i).scl((float) perp))).scl(1);
float newAngle = getAngle2Vecs(ball.getVelocity(), pad.returnNormal(i));
//Gdx.app.log("Angle", newAngle + "");
ball.setVelocity(new Vector2(gameWidth / 2 - ball.getColCircle().x, gameHeight / 2 - ball.getColCircle().y));
int rand = (int) Math.random() * 90 + 5;
if (pad.getAngularVelocity() < 0) {
ball.setVelocity(ball.getVelocity().cpy().rotate((float) (rand + Math.random() * 50)));
} else if (pad.getAngularVelocity() > 0) {
ball.setVelocity(ball.getVelocity().cpy().rotate((float) (-rand - Math.random() * 50)));
} else {
ball.setVelocity(ball.getVelocity().cpy().rotate(Math.random() < 0.5 ? -rand : rand));
}
if (score <= 5 && input.getModes() == 'e') {
ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_0));
} else if (score >= 5 && score < 50 && input.getModes() == 'e') {
ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_5));
} else if (score >= 10 && score < 50 && input.getModes() == 'e') {
ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_10));
} else if (score >= 20 && score < 50 && input.getModes() == 'e') {
ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_20));
} else if (score >= 35 && score < 50 && input.getModes() == 'e') {
ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_35));
} else if (score >= 50 && score < 75 && input.getModes() == 'e') {
ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_50));
} else if (score >= 65 && score < 75 && input.getModes() == 'e') {
ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_65));
} else if (score >= 75 && score < 100 && input.getModes() == 'e') {
ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_75));
} else {
ball.setVelocity(ball.getVelocity().cpy().scl(Configuration.E_VELOCITY_OVER_100));
}
//Gdx.app.log("VEL",ball.getVelocity().len() + "");
//EFFECTS
ball.paddleCollide();
centerCircle.paddleCollide();
if (secondValue.getValue() == 1) {
score++;
AssetLoader.bounce.play();
}
secondValue.setValue(0);
Tween.to(secondValue, -1, 0.1f).target(1).repeatYoyo(0, 0)
.ease(TweenEquations.easeInSine).start(manager);
}
} else {
for (int i = 0; i < pad.getcolCircles().size(); i++) {
if (!Intersector.overlaps(pad.getcolCircles().get(i), ball.getColCircle())) {
ball.setCollided(false);
} else {
//ball.setCollided(true);
}
}
}
}
if (!Intersector.overlaps(ball.getColCircle(), new Rectangle(-50, 50, gameWidth + 50, gameHeight + 50)) && !finish) {
finishGame();
}
}
问题在于碰撞:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at gameworld.GameWorld.collisions(GameWorld.java:151)
at gameworld.GameWorld.update(GameWorld.java:119)
at screens.GameScreen.render(GameScreen.java:34)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
因为我在碰撞中将input.getModes() == 'e'
行添加到了if
方法中。
以下是输入代码:
public void setModes (char mode){
this.mode = mode;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
screenX = scaleX(screenX);
screenY = scaleY(screenY);
if (menuButtons.get(0).isTouchUp(screenX, screenY)) {
world.getMenuObject().getPad().end();
setModes('e');
for (int i = 0; i < menuButtons.size(); i++) {
menuButtons.get(i).end();
}
world.getMenuObject().getVolumeButton().end();
menuButtons.get(0).tranToGameScreen();
}if (menuButtons.get(1).isTouchUp(screenX, screenY)) {
world.getMenuObject().getPad().end();
setModes('m');
for (int i = 0; i < menuButtons.size(); i++) {
menuButtons.get(i).end();
}
world.getMenuObject().getVolumeButton().end();
menuButtons.get(1).tranToGameScreen();
}if (menuButtons.get(2).isTouchUp(screenX, screenY)) {
world.getMenuObject().getPad().end();
setModes('h');
for (int i = 0; i < menuButtons.size(); i++) {
menuButtons.get(i).end();
}
world.getMenuObject().getVolumeButton().end();
menuButtons.get(2).tranToGameScreen();
} else if (menuButtons.get(3).isTouchUp(screenX, screenY)) {
world.getActionResolver().showScores();
} else if (menuButtons.get(4).isTouchUp(screenX, screenY)) {
world.getActionResolver().shareGame("Try " + Configuration.gameName + "!! ");
} else if (menuButtons.get(5).isTouchUp(screenX, screenY)) {
world.getActionResolver().showAchievement();
} else if (menuButtons.get(6).isTouchUp(screenX, screenY)) {
world.getActionResolver().rateGame();
}
return false;
}
public char getModes (){
return mode;
}
有人能帮忙吗?
答案 0 :(得分:1)
您使用它时,input
很可能是null
。您可以通过在System.out.println(input);
出现的行之前添加NullPointerException
来检查此问题。如果它是控制台是null
而不是我是对的。
您应该在使用之前初始化input
。像这样:
input = new InputHandlerMenu();