2014-01-27 20 views
1

編輯:
按照每的回答是:我說這個和它的作品罰款:使用ClickListener檢測actor上的clic:無響應?

private class GameScreen implements Screen { 
     private Stage mStage; 
     private InputMultiplexer inputMultiplexer = new InputMultiplexer(); 

     public GameScreen() { 
      Gdx.input.setInputProcessor(inputMultiplexer); 
      mStage = new Stage(0, 0, true); 
      MyInput mi = new MyInput(){ //which implements inputProcessor 
       @Override 
       public boolean touchDown(int screenX, int screenY, int pointer, int button) { 
        General.Log("gamescreen touchDown"); 
        return false; 
       } 

       @Override 
       public boolean touchDragged(int screenX, int screenY, int pointer) { 
        // TODO Auto-generated method stub 
        return false; 
       } 
      }; 
      inputMultiplexer.addProcessor(mi); 
      inputMultiplexer.addProcessor(mStage); 
     } 

我想檢測的UI演員點擊,我註冊stage作爲inputProcessor

Gdx.input.setInputProcessor(stage); 

我把這段話添加到我的演員:

setBounds(0, 0, texture.getWidth(), texture.getHeight()); 

但是仍然沒有迴應...

回答

1

很難在沒有獲得有關您的設置的更多信息的情況下爲您提供幫助。但是這段代碼片段對我來說很好,看看你做了什麼不同。如果你不能正確地創建一個只有失敗的代碼的小項目,那麼我們更容易幫助你。

import com.badlogic.gdx.Game; 
    import com.badlogic.gdx.Gdx; 
    import com.badlogic.gdx.Screen; 
    import com.badlogic.gdx.graphics.GL20; 
    import com.badlogic.gdx.graphics.Texture; 
    import com.badlogic.gdx.graphics.Texture.TextureFilter; 
    import com.badlogic.gdx.graphics.g2d.TextureRegion; 
    import com.badlogic.gdx.scenes.scene2d.InputEvent; 
    import com.badlogic.gdx.scenes.scene2d.Stage; 
    import com.badlogic.gdx.scenes.scene2d.ui.Image; 
    import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; 

    public class MyGame extends Game { 

     @Override 
     public void create() { 
      setScreen(new GameScreen()); 
     } 

     private class GameScreen implements Screen { 

      private Stage mStage; 

      public GameScreen() { 
       mStage = new Stage(0, 0, true); 
      } 

      @Override 
      public void render(float delta) { 
       mStage.act(delta); 
       Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
       mStage.draw(); 
      } 

      @Override 
      public void resize(int width, int height) { 
       Gdx.gl.glViewport(0, 0, width, height); 
       mStage.setViewport(width, height); 
      } 

      @Override 
      public void show() { 
       Texture texture = new Texture(Gdx.files.internal("data/libgdx.png")); 
       texture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 
       TextureRegion region = new TextureRegion(texture, 0, 0, 40, 40); 

       for (int y = 0; y < 4; ++y) { 
        for (int x = 0; x < 4; ++x) { 
         Image imageActor = new Image(region); 
         imageActor.setPosition(x * 50, y * 50); 
         imageActor.addListener(new ClickListener() { 
          public void clicked(InputEvent event, float x, float y) { 
           System.out.println("clicked"); 
          } 
         }); 
         mStage.addActor(imageActor); 
        } 
       } 

       Gdx.input.setInputProcessor(mStage); 
      } 

      @Override 
      public void hide() {} 

      @Override 
      public void pause() {} 

      @Override 
      public void resume() {} 

      @Override 
      public void dispose() {} 
     } 
    } 

用了ApplicationListener:

import com.badlogic.gdx.ApplicationListener; 
    import com.badlogic.gdx.Gdx; 
    import com.badlogic.gdx.graphics.GL20; 
    import com.badlogic.gdx.graphics.Texture; 
    import com.badlogic.gdx.graphics.Texture.TextureFilter; 
    import com.badlogic.gdx.graphics.g2d.TextureRegion; 
    import com.badlogic.gdx.scenes.scene2d.InputEvent; 
    import com.badlogic.gdx.scenes.scene2d.Stage; 
    import com.badlogic.gdx.scenes.scene2d.ui.Image; 
    import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; 

    public class MyGame implements ApplicationListener { 

     private Stage mStage; 

     @Override 
     public void create() { 
      mStage = new Stage(0, 0, true); 

      Texture texture = new Texture(Gdx.files.internal("data/libgdx.png")); 
      texture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 
      TextureRegion region = new TextureRegion(texture, 0, 0, 40, 40); 

      for (int y = 0; y < 4; ++y) { 
       for (int x = 0; x < 4; ++x) { 
        Image imageActor = new Image(region); 
        imageActor.setPosition(x * 50, y * 50); 
        imageActor.addListener(new ClickListener() { 
         public void clicked(InputEvent event, float x, float y) { 
          System.out.println("clicked"); 
         } 
        }); 
        mStage.addActor(imageActor); 
       } 
      } 

      Gdx.input.setInputProcessor(mStage); 
     } 

     @Override 
     public void resize(int width, int height) { 
      Gdx.gl.glViewport(0, 0, width, height); 
      mStage.setViewport(width, height); 
     } 

     @Override 
     public void render() { 
      mStage.act(Gdx.graphics.getDeltaTime()); 
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
      mStage.draw();   
     } 

     @Override 
     public void pause() {} 

     @Override 
     public void resume() {} 

     @Override 
     public void dispose() {}  
    } 
+0

謝謝你,是有辦法了'Stage'的一類也實現'ApplicationListener'結合?你的例子工作正常。當我添加一個類,'Test test = new Test();'實現ApplicationListener時,它不起作用:例如'render'什麼也不顯示。 'Gdx.input.setInputProcessor'已經被用於'mStage',但是我需要檢測舞臺(用於UI)和世界(例如拖動正交相機)的觸摸。任何想法? – Paul

+1

我剛剛用一個使用ApplicationListener的例子更新了代碼。 –

+0

非常感謝您的幫助,所以我想我應該只使用一個inputProcessor,我認爲錯誤必須來自此,而不是applicationListener。我會試一試,我應該接近解決方案:)謝謝 – Paul