2016-11-10 9 views
0

私はゲームでタッチダウン()を使用しようとしています。動機は、私はちょうど、画面上のタッチ検出に基づいてプレイヤーを移動したい、しかし、それは次のエラーを与えている、シンプルである:libGDXのタッチダウンを使用

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


    } 

} 

と私は入力を設定していた:

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) 

とコードがありますロンクラスのオブジェクトロンを使用している別のクラス内のプロセッサは、このような(クラスは、上に示した):

Gdx.input.setInputProcessor(ron); 

答えて

0

39

ラインはあります
worldClick = viewport.unproject(new Vector2(screenX, screenY)); 

のでNullPointerExceptionはかなり程度viewport変数である - あなたがそうおそらく、ビューポートがnull値が設けられているコンストラクタ

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

にこれを渡しています。

は、第二のものは多分フラグを使用して、この場合には最高のアイデアではないということです What is a NullPointerException, and how do I fix it?


を見てみましょう - あなたのコードは間違いなく読みにくくあり、また、あなたが冗長不要なコードを持っています。

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

より良いは、ちょうどあなたが

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

を行うようにあなたも簡単のような(チェンジ・ベロシティ・ロジックをこのように実装することができupdateでデルタを掛け方向ベクトル速度のを設定することですキャラクターが方向を変えるとスローダウンしてから後ろに移動し始めます...など)

+0

私は可読性の問題で申し訳ありません、私は初心者です。交換。あなたが正しく、ビューポートに関する問題がありました。私の実際の動機は、タッチに基づいてキャラクターを右または左に動かすことでした(タッチが画面の右半分に、次に右半分、左半分が左の場合)。しかし、このコードは、ビューポートを修正した後でさえも機能しません。 –

関連する問題