2016-02-09 113 views
3

我是LibGdx新手,並試圖讓我的iceCream圖像觸摸。 我想知道如何設置輸入過程(通過觸摸屏幕)。 我需要另外上課嗎?當我嘗試將我的Prac1類的輸入過程實現爲 時,JAVA不允許我在不更改類摘要的情況下實現。具體而言,我喜歡每當用戶觸摸圖像 時,它都會計算觸摸的次數。這是我的代碼,謝謝你的幫助。LibGdx,如何處理觸摸事件?

import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 

public class Prac1 extends ApplicationAdapter { 
    int w,h,tw,th =0; 
    OrthographicCamera camera; 
    SpriteBatch batch; 
    Texture img; 

    @Override 
    public void create() { 
     w = Gdx.graphics.getWidth(); 
     h = Gdx.graphics.getHeight(); 
     camera = new OrthographicCamera(w, h); 
     camera.position.set(w/2, h/2, 0); 
     camera.update(); 
     batch = new SpriteBatch(); 
     img = new Texture(Gdx.files.internal("iceCream.png")); 
     tw = img.getWidth(); 
     th = img.getHeight(); 
    } 

    @Override 
    public void render() { 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     batch.begin(); 
     batch.draw(img, camera.position.x - (tw/2), camera.position.y - (th/2)); 
     batch.end(); 
    } 
} 

回答

5

您可以使用

InputProcessor
處理用戶輸入。 像這樣: -

import com.badlogic.gdx.ApplicationAdapter; 
import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.InputAdapter; 
import com.badlogic.gdx.graphics.GL20; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.Texture; 
import com.badlogic.gdx.graphics.g2d.Sprite; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 

public class Prac1 extends ApplicationAdapter { 
    float w,h,tw,th =0; 
    OrthographicCamera camera; 
    SpriteBatch batch; 
    Sprite img; 

    @Override 
    public void create() { 
     w = Gdx.graphics.getWidth(); 
     h = Gdx.graphics.getHeight(); 
     camera = new OrthographicCamera(w, h); 
     camera.position.set(w/2, h/2, 0); 
     camera.update(); 
     batch = new SpriteBatch(); 
     img = new Sprite(new Texture(Gdx.files.internal("iceCream.png"))); 

     tw = img.getWidth(); 
     th = img.getHeight(); 
     img.setBounds(camera.position.x - (tw/2), camera.position.y - (th/2),tw,th); 
     Gdx.input.setInputProcessor(new InputAdapter(){ 

      @Override 
      public boolean touchDown(int screenX, int screenY, int pointer, int button) { 

       if(img.getBoundingRectangle().contains(screenX, screenY)) 
         System.out.println("Image Clicked"); 

       return true; 
      } 

     }); 
    } 

    @Override 
    public void render() { 
     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     batch.begin(); 
     img.draw(batch); 
     batch.end(); 
    } 
} 

替換你的代碼這個代碼,你可以很容易地理解這裏發生了什麼。 你也可以實現

GestureListener
來處理手勢事件。

+1

哦,我想你的答案是正確的:) – IAMBEAST

2

由於您需要從圖像中獲取觸摸事件,因此可以使用舞臺和演員來完成。你需要創建一個舞臺上,並與Image你的紋理,然後加入可觸摸的屬性:

 iceCreamImg.setTouchable(Touchable.enabled); 
     iceCreamImg.addListener(new InputListener() { 
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
      Gdx.app.debug(TAG, "touchDown()"); 
      // must return true for touchUp event to occur 
      return true; 
     } 
     public void touchUp (InputEvent event, float x, float y, int pointer, int button) { 
      Gdx.app.debug(TAG, "touchUp()"); 
     } 
    }); 

,並添加圖片到舞臺上。在渲染方法你應該增加:

stage.act(); 
stage.draw(); 

,還可以設置輸入處理器爲你的舞臺與

Gdx.input.setInputProcessor(stage);