2013-05-19 65 views
0

我一直在使用libgdx實現touchDragged偵聽器。如何實現libgdx touchDragged?

這是我的代碼,任何人都可以建議如何拖動圖像時,用戶觸摸它,而不是移動手指?

我使用舞臺和演員,我想捕捉演員的touchDragged事件。

謝謝。

public void create() { 
    Texture.setEnforcePotImages(false); 

    stage = new Stage(); 
    Gdx.input.setInputProcessor(stage); 
    // create a SpriteBatch with which to render the sprite 
    batch = new SpriteBatch(); 

    // load the sprite's texture. note: usually you have more than 
    // one sprite in a texture, see {@see TextureAtlas} and {@see TextureRegion}. 
    texture = new Texture(Gdx.files.internal("ball3.png")); 
    Skin skin = new Skin(); 
    skin.add("default", new Label.LabelStyle(new BitmapFont(), Color.WHITE)); 
    skin.add("ball", texture); 
    Image sourceImage = new Image(skin, "ball"); 
    sourceImage.setBounds(50, 125, 100, 100); 
    stage.addActor(sourceImage); 

    // create an {@link OrthographicCamera} which is used to transform 
    // touch coordinates to world coordinates. 
    camera = new OrthographicCamera(); 

    // we want the camera to setup a viewport with pixels as units, with the 
    // y-axis pointing upwards. The origin will be in the lower left corner 
    // of the screen. 
    camera.setToOrtho(false); 
} 

public void render() { 
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
    stage.act(Gdx.graphics.getDeltaTime()); 
    stage.draw(); 
    Table.drawDebug(stage); 



    // if a finger is down, set the sprite's x/y coordinate. 
    if (Gdx.input.isTouched()) { 
     // the unproject method takes a Vector3 in window coordinates (origin in 
     // upper left corner, y-axis pointing down) and transforms it to world 
     // coordinates. 
     camera.unproject(spritePosition.set(Gdx.input.getX(), Gdx.input.getY(), 0)); 
    } 
} 

回答

2

步驟如下:

  1. 您需要檢查,如果手指觸摸要移動的對象。下面是一些有用的方法,讓您的手指的位置:

    Gdx.input.getX(); 
    Gdx.input.getY(); 
    
  2. 你需要使用一個變量來跟蹤手指是否移動,觸摸時。

  3. 如果變量爲true,則將對象的位置更改爲手指位置。

  4. 當手指不再觸摸屏幕時,可以禁用變量。

1

我建議你使用InputProcessor:

public class MyInputProcessor implements InputProcessor{ 
    @Override 
    public boolean keyDown(int keycode){ 
     return false; 
    } 
    @Override 
    public boolean keyUp(int keycode){ 
     return false; 
    } 
    @Override 
    public boolean touchDown(int screenX, int screenY, int pointer, int button){ 
     return false; 
    } 
    @Override 
    public boolean touchUp(int screenX, int screenY, int pointer, int button){ 
     return false; 
    } 
    @Override 
    public boolean keyTyped(char character){ 
      return false; 
    } 
    @Override 
    public boolean touchDragged(int screenX, int screenY, int pointer){ 
     return false; 
    } 
    @Override 
    public boolean mouseMoved(int screenX, int screenY){ 
     return false; 
    } 
    @Override 
      public boolean scrolled(int amount) { 
        return false; 
      } 
} 

使它成爲一個領域:

MyInputProcessor inputProcessor; 

而在的onCreate():

inputProcessor = new MyInputProcessor(); 
Gdx.input.setInputProcessor(inputProcessor); 

這樣你可以只需在touchDragged回調中實現你的代碼即可。