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