2016-11-10 29 views
0

我想在我的遊戲中使用touchdown()。動機很簡單,我只想根據屏幕上的觸摸檢測移動播放器,但它給了以下錯誤:在libGDX中使用觸點

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) 

而且代碼:

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


    } 

} 

和我一組輸入其中使用羅恩類的對象羅恩另一個類處理器(上面顯示的類)是這樣的:

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; 
更好

將設定速度的方向向量然後就在update通過增量乘以像你一樣

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

你也可以輕鬆實現轉換速度邏輯這種方式(如當字符改變方向他慢下來,然後開始向後移動...等)

+0

哈哈,是啊對於可讀性問題抱歉,我是一個初學者,也是新的堆棧交換。你說得對,視口有問題。我的動機是基於觸摸向右或向左移動角色(如果觸摸在屏幕的右半部分然後右側,並且如果另一半離開)。但是,即使糾正視口後,此代碼仍然無法正常工作。任何見解? –