如何检查背景是否被触及而不是libgdx中的Actor?

时间:2014-07-01 18:50:44

标签: java android libgdx actor

我的屏幕上一遍又一遍地有随机的盒子,当盒子触摸时它会切换位置。我不知道如何检查屏幕是否被触摸而不是框。除了盒子之外的任何地方。

    private Stage stage;
    private Texture boxImage;
    private Image pop;


@Override
public void show() {
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    boxImage = new Texture(Gdx.files.internal("box.png"));
    pop = new Image(boxImage);


    pop.setPosition(20,20);

     pop.addListener(new ClickListener(){
         @Override
        public void clicked(InputEvent event, float x, float y)
         {
                int num1 = (int)MathUtils.random(50,500);
                int num2 = (int)MathUtils.random(50,500);
                pop.setPosition(num1,num2);

         }
     });

    stage.addActor(pop);

}
@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0 , 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(delta);
    stage.draw();

}

1 个答案:

答案 0 :(得分:0)

如何只将ClickListener添加到舞台上?

    stage.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {

            //check if box is hit
            if (pop.equals(stage.hit(x, y, false))) {
                //box hit
            } else {
                //box was not hit
            }

        }
    });