子弹碰撞检测

时间:2012-06-02 10:53:57

标签: android box2d sprite andengine bullet

我正在用andengine开发一款游戏。我有玩家。还有随机时间与MoveModifier一起出现的敌人,他们是精灵。我想:玩家射击子弹,子弹接触敌人,敌人将是看不见。我该怎么办?

子弹

  private Runnable mStartCircle1 = new Runnable() {

            public void run() {
                final Scene scene= HelicopterActivity.this.mEngine.getScene();



                 float velocityOfCircleC=1.6f;
                    float startXC=player.getX()+50;
                    float finishXC=800;
                    float startYC=player.getY()+15;
                    float finishYC=startYC;

                    bullet=new Sprite(startXC, startYC, HelicopterActivity.this.gasTextureRegion);
                    bullet.registerEntityModifier(
                            (IEntityModifier) new SequenceEntityModifier (
                                    new MoveModifier(velocityOfCircleC, bullet.getX(),  finishXC, bullet.getY(), finishYC)));

                    scene.attachChild(bullet);

敌人

      int j=0, k=0;
    private Runnable mStartCircle = new Runnable() {

        public void run() {
            final Scene scene= HelicopterActivity.this.mEngine.getScene();

            float startY=randomNumber.nextInt(670 - 1); 
            float velocityOfCircle=6;
            enemy[j] = new Sprite( 800, startY, HelicopterActivity.this.enemyTextureRegion);
            enemy[j].registerEntityModifier(
                    (IEntityModifier) new SequenceEntityModifier (
                                new MoveModifier(velocityOfCircle, enemy[j].getX(),  -128, 
                                        enemy[j].getY(), enemy[j].getY())));
            scene.attachChild(enemy[j]);

            float startY2=randomNumber.nextInt(670 - 1); 
            float velocityOfCircle2=10;
            enemy2[k] = new Sprite( 800, startY2, HelicopterActivity.this.enemy2TextureRegion);
            enemy2[k].registerEntityModifier(
                    (IEntityModifier) new SequenceEntityModifier (
                                new MoveModifier(velocityOfCircle2, enemy2[k].getX(),  -128, 
                                        enemy2[k].getY(), enemy2[k].getY())));
            scene.attachChild(enemy2[k]);






            k++;
            j++;

            if (k<50){
                mHandler.postDelayed(mStartCircle,5000);
            } 

     }};


                if (i<1000){
                    mHandler.postDelayed(mStartCircle1,1000);
                } 

         }};

2 个答案:

答案 0 :(得分:2)

这是与你类似的游戏......在这些例子中是碰撞检测示例:

Simple android game tutotrial using andengine

Improved version of the game

答案 1 :(得分:1)

检测碰撞的一种方法是将子弹和敌人都视为矩形并检测矩形是否发生碰撞,检查this以了解如何检测矩形之间的碰撞。

否则,请查看this教程,了解如何检测任何形状的两个物体之间的碰撞。