java - Using touchdown in libGDX -


i trying use touchdown() in game. motive simple, want move player based on touch detection on screen, giving following error:

    exception in thread "lwjgl application" java.lang.nullpointerexception @ com.mygdx.game.sprites.ron.touchdown(ron.java:39) @ com.badlogic.gdx.backends.lwjgl.lwjglinput.processevents(lwjglinput.java:329) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication.mainloop(lwjglapplication.java:215) @ com.badlogic.gdx.backends.lwjgl.lwjglapplication$1.run(lwjglapplication.java:124) 

and 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 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 had set input processor in class using object ron of ron class(the class shown above) this:

gdx.input.setinputprocessor(ron);  

39 line

worldclick = viewport.unproject(new vector2(screenx, screeny)); 

so nullpointerexception rather viewport variable - passing constructor

public ron(int x, int y, extendviewport viewport) {     ...     this.viewport = viewport;     ... 

so viewport being provided null value.

take @ what nullpointerexception, , how fix it?


the second thing maybe in case using flags not best idea - code less readable, have redundant unnecessary code.

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

better set direction vector of velocity multiply delta in update do

//in touchdown if (worldclick.x >= viewport.getworldwidth() / 4) {     velocity.x *= (velocity.x >= 0) ? 1 : -1; } //also add else here - there 1 or here else if (worldclick.x < viewport.getworldwidth() / 4) {     velocity.x *= (velocity.x < 0) ? 1 : -1; }  //in update position.x += delta * velocity.x; 

you can implement change-velocity logic way (like when character changes direction slow down starting move backward... etc)


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -