0

I am trying to use touchdown() in my game. Motive is simple, I just want to move the player based on touch detection on screen, But it is giving the following error:

    Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.mygdx.game.sprites.Ron.touchDown(Ron.java:39)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:329)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

and the code is:

package com.mygdx.game.sprites;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.Constants;

/**
 * Created by Vamsi Rao on 11/9/2016.
 */

public class Ron extends InputAdapter {
    private Vector2 position;
    private Vector2 velocity;
    Vector2 worldClick;
    boolean right;
    boolean left;

    private ExtendViewport viewport;

    private Texture ron;

    public Ron(int x, int y, ExtendViewport viewport) {
        super();
        position = new Vector2(x, y);
        velocity = new Vector2(Constants.VELOCITY_X, 0);
        this.viewport = viewport;

        ron = new Texture("ron.png");
    }

    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        worldClick = viewport.unproject(new Vector2(screenX, screenY));
        if (worldClick.x >= viewport.getWorldWidth() / 4) {
            right = true;
        }
        if (worldClick.x < viewport.getWorldWidth() / 4) {
            left = true;
        }
        return true;
    }




    public Vector2 getPosition() {
        return position;
    }

    public Texture getTexture() {
        return ron;
    }

    public void update(float delta) {
        left=false;
        right=false;


        if (right) {
            position.x += delta * velocity.x;
        }

        if (left) {
            position.x -= delta * velocity.x;
        }


    }

}

and I had set Input processor in another class which is using the object ron of Ron class(the class shown above) like this:

Gdx.input.setInputProcessor(ron); 
Vamsi Rao
  • 27
  • 5

1 Answers1

0

39 line is

worldClick = viewport.unproject(new Vector2(screenX, screenY));

so the NullPointerException is rather about viewport variable - you are passing this to the constructor

public Ron(int x, int y, ExtendViewport viewport) {
    ...
    this.viewport = viewport;
    ...

so probably the viewport is being provided with null value.

Take a look at What is a NullPointerException, and how do I fix it?


The second thing is that maybe in this case using flags is not the best idea - your code is definitely less readable, also you have a redundant unnecessary code.

//these lines are almost the same
position.x += delta * velocity.x;
position.x -= delta * velocity.x;

better would be to set the direction vector of velocity then just multiply it by delta in update like you do

//in your touchdown
if (worldClick.x >= viewport.getWorldWidth() / 4) {
    velocity.x *= (velocity.x >= 0) ? 1 : -1;
}
//also add else here - there is always one or another here
else if (worldClick.x < viewport.getWorldWidth() / 4) {
    velocity.x *= (velocity.x < 0) ? 1 : -1;
}

//in update
position.x += delta * velocity.x;

you can also easily implement change-velocity logic this way (like when character changes direction he slow down then starting to move backward... etc)

Community
  • 1
  • 1
m.antkowicz
  • 13,268
  • 18
  • 37
  • Haha, yeah sorry for the readability issue,I am a beginner and also new to Stack exchange. You were right, there was an issue with viewport. My actual motive was to move the character right or left based on the touch(if touch is on the right half of the screen then right and if other half then left). But this code is not working for that even after correcting viewport.Any insight? – Vamsi Rao Nov 10 '16 at 14:49