0

基本上,精灵每(1,2 或 3 秒)随机生成一次,并且无限次生成。我希望精灵在屏幕上触摸后消失。(安卓触摸事件)

public void newEnemy(){
        Sprite newEnemy=Pools.obtain(Sprite.class);
        newEnemy.set(enemy);
        newEnemy.setPosition(200, 700);
        enemies.add(newEnemy);
    }

public void update(){
        deltaTime=Gdx.graphics.getDeltaTime();
        timer+=1*deltaTime;
        timer2+=1*deltaTime;
        timer3+=1*deltaTime;

        if(timer>=random){
            newEnemy();  //spawn a new enemy
            timer-=random;
            random=rTime.nextInt(3)*1f+1;//create random time if timer>= initial random time;
        }
4

1 回答 1

1

您将需要设置一个触摸侦听器。相关信息在这里

然后,您需要检查触摸位置是否在您的精灵边界内。一种常见的方法是创建一个矩形并检查触摸位置是否在矩形内部,如下所示

 Rectangle2D bounds = new Rectangle2D.Float(x, y, width, height);

`if(bounds.contains(`the touch x value`,` the touch y value`){`

         //your code to remove the sprite
    }

或者,您可以在 sprite 中编写自己的方法,如果您只需要 contains 方法,这将是一个更好的决定。这样,您不必导入另一个库。(请注意,这并没有太大的区别,但这是一种很好的做法)

public boolean contains(int x, int y) {
     return (x > this.x && y > this.y && x < this.x + this.width && y < this.y + this.height);
}
于 2015-05-23T14:42:16.713 回答