android - 线程 "LWJGL Application"中的异常

标签 android libgdx

我创建了 3 个按钮:“困难”、“中等”和“简单”。在三种模式中的每一种模式下,速度都会发生变化,并且按钮会起作用。

问题是当我将这一行添加到代码时:&& 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)

因为我在冲突中的 if 方法中添加了 input.getModes() == 'e' 行。

输入代码如下:

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

有人能帮忙吗?

最佳答案

在您使用它的那一刻,您的 input 很可能是 null。您可以通过在发生 NullPointerException 的行之前添加 System.out.println(input); 来检查这一点。如果它输出到控制台是 null,那么我是对的。

您应该在使用之前初始化您的输入。像这样:

input = new InputHandlerMenu();

关于android - 线程 "LWJGL Application"中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28916045/

相关文章:

android - 无法同步gradle

java - 从 firebase 中检索随机节点

android - 使用深层链接从另一个启动应用程序

java - 如何从 PagerAdapter 类更改 fragment 的 TextView

java - Actor 职务

android - libgdx:android 应用程序找不到主游戏类

java - 删除后接触处理程序中的 Box2D 空夹具

android - native 应用程序登录

java - 在纹理中切割一个半透明的正方形

java.lang.NoClassDefFoundError : android/os/Build$VERSION 错误