Reputation: 67
I've created 3 buttons: 'hard', 'medium', and 'easy'. On each of the three modes, the velocity changes, and the buttons work.
The problem is when I add this line to the code : && input.getModes() == 'e'
. Once this line is added, when I click on any of the velocity buttons, it stops.
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();
}
}
The problem is on the collisions:
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)
Because I added the line input.getModes() == 'e'
into the if
method in collision.
Here is the code if input:
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;
}
Is anyone able to help?
Upvotes: 1
Views: 100
Reputation: 649
Most probably your input
is null
at the moment you are using it. You can check this by adding System.out.println(input);
before the line at which NullPointerException
occurs. If it's out to console is null
than I'm right.
You should initialize your input
before using it. Something like this:
input = new InputHandlerMenu();
Upvotes: 1