0

我创建了一个简单的测试,其中touchDown()包含图像演员的事件。该事件在舞台上有效,但对演员无效。这是代码:

InputProcessor在父类(AbstractScreen)的构造函数中设置了。

Gdx.input.setInputProcessor(this.stage);

在子类中:

private TextureAtlas atlas;
private Image boardImg;

public void show(){
    super.show(); 

    atlas = new TextureAtlas(Gdx.files.internal("data/packs/mainMenu.atlas"));      
    AtlasRegion board = atlas.findRegion("board");

    boardImg = new Image(board);

    boardImg.addListener(new InputListener(){
           public boolean touchDown(InputEvent event, float x, float y, int pointer, int    button){
               Gdx.app.log("aaaa", "down");
               return true;
           }
    });

    stage.addActor(boardImg);

    stage.addListener(new InputListener() {
          public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
               Gdx.app.log("aaaa", "stage down");
               return true;
          }
    });     
}


@Override
public void resize(int width, int height) {
    super.resize(width, height);        
    boardImg.setPosition(stage.getWidth()/2 - boardImg.getWidth()/2, stage.getHeight()/4);  
}

我在桌面和安卓上都进行了测试(结果相同)。我得到了“下台”事件,但没有得到演员的事件。我也试过没有舞台活动。

4

3 回答 3

0

图像不接收触摸事件,因为它没有大小。

仔细阅读文档:

请注意,actor 必须指定其边界才能在这些边界内接收输入事件。

只知道boardImg = new Image(board)不会为您设置演员的宽度和高度。所以你需要手动制作。假设您使用像素完美尺寸:

boardImg = new Image(board);
boardImg.setWidth(board.getWidth());
boardImg.setHeight(board.getHeight());

那会成功的。祝你好运。

于 2014-08-12T19:59:06.363 回答
0

完成所有这些工作:

  1. boardImg.setTouchable(Touchable.enabled)

  2. 在舞台侦听器中返回 false 或清除其侦听器。

  3. 不要在父类中调用 setInputProcessor(this.stage) 而是在子类中调用。

于 2012-11-09T21:21:19.760 回答
0

我认为问题是因为touchDown()stage 的方法返回true,所以事件将被“消费”并且不会传播给它的孩子,演员,所以不会调用touchDown()方法。boardImg

于 2012-11-10T19:50:07.960 回答