2013-05-04 151 views
0

我在AndEngine中開發我幾乎完成了我的遊戲,但不幸的是,由於我在更新循環中檢查.collides很多,偶爾會遇到低fps。我現在明白,這是問題所在,我一直試圖用Box2d來改變它與理論上的物體,這正是我所需要的,但我無法抓住它!Box2d碰撞檢測與陣列

基本上,我有4個數組;一個是汽車,三個是敵人。汽車從左到右駕駛,如果他們與這些敵人中的任何一個接觸,則速度會有所不同。當我加載陣列時,是否必須爲每輛汽車和敵人分配車身?然後我該如何檢查?我是否運行for循環,然後運行isBodyContacted(carBody,iceBergBody);'在更新中看?這有點合作!

僅供參考,裝我的車:

private void loadCar() { 
      for (int i = 0; i < rManager.getInstance().carArray.length; i++) { 
        rManager.getInstance().carArray[i] = new Car(new Sprite(
            rManager.getInstance().spawnPoint[i].getSpawnPos().x, 
            rManager.getInstance().spawnPoint[i].getSpawnPos().y, 
            rManager.getInstance().car_region, 
            engine.getVertexBufferObjectManager()) { 
          @Override 
          public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, 
              final float pTouchAreaLocalX, 
              final float pTouchAreaLocalY) { 

            if (pSceneTouchEvent.isActionMove() 
                && GameManager.getInstance().getDebugMode() == true) { 
              this.setPosition(pSceneTouchEvent.getX(), 
                  pSceneTouchEvent.getY()); 
            } 
            return true; 
          } 
        }); 
      } 

      for (int i = 0; i < rManager.getInstance().carArray.length; i++) { 
        rManager.getInstance().carArray[i].getCarSprite().setScale(0.5f); 
        rManager.getInstance().carArray[i].getCarSprite().setAnchorCenter(
            0, 0); 
        colourX = (randNumber(2, 9)); 
        colourY = (randNumber(2, 9)); 
        colourZ = (randNumber(2, 9)); 
        colourX /= 10; 
        colourY /= 10; 
        colourZ /= 10; 
        rManager.getInstance().carArray[i].getCarSprite().setColor(colourZ, 
            colourY, colourZ); 
        attachChild(rManager.getInstance().carArray[i].getCarSprite()); 
        this.registerTouchArea(rManager.getInstance().carArray[i] 
            .getCarSprite()); 
        this.setTouchAreaBindingOnActionDownEnabled(true); 
      } 
    } 

加載一個敵人..

void loadIceBerg() { 
      for (int i = 0; i < rManager.getInstance().iceBergArray.length; i++) { 
        rManager.getInstance().iceBergArray[i] = new IceBergEnemy(
            new Sprite(randNumber(200, 700), randNumber(0, 480), 
                rManager.getInstance().iceBerg_region, 
                engine.getVertexBufferObjectManager()) { 
              @Override 
              public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) { 
                this.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY()); 
                if (this.getY() > rManager.getInstance().camera.getHeight() - (this.getHeight()/2) || this.getY() < 0 + (this.getHeight()/2)) 
                { 
                  this.setPosition(200 - (pDifficultyLevel * 20), 
                      -100); 
                                  showScore(50, " ice berg!"); 
                  this.setVisible(false); 

                }  

                if(pSceneTouchEvent.isActionUp()) 
                { 
                  updateScore(50); 
                  GameManager.getInstance().updateTotalIceBergEnemiesKilled(); 

                } 



                return true; 
              } 
            }); 

        rManager.getInstance().iceBergArray[i].getIceBergSprite() 
            .setPosition(
                rManager.getInstance().iceBergArray[i] 
                    .getIceBergSprite().getX(), 
                rManager.getInstance().iceBergArray[i] 
                    .getIceBergSprite().getY()); 
        rManager.getInstance().iceBergArray[i].getIceBergSprite().setScale(
            0.8f); 
        this.registerTouchArea(rManager.getInstance().iceBergArray[i] 
            .getIceBergSprite()); 
        this.setTouchAreaBindingOnActionDownEnabled(true); 

        attachChild(rManager.getInstance().iceBergArray[i] 
            .getIceBergSprite()); 
      } 
    } 

一些碰撞!

else if (rManager.getInstance().iceBergArray[2] 
                .getIceBergSprite().collidesWith(
                    rManager.getInstance().carArray[r] 
                        .getCarSprite())) { 
              rManager.getInstance().carArray[r].setCarSpeed(0f); 
            } 

            else if (rManager.getInstance().iceBergArray[3] 
                .getIceBergSprite().collidesWith(
                    rManager.getInstance().carArray[r] 
                        .getCarSprite())) { 
              rManager.getInstance().carArray[r].setCarSpeed(0f); 
            } 

            else if (rManager.getInstance().iceBergArray[4] 
                .getIceBergSprite().collidesWith(
                    rManager.getInstance().carArray[r] 
                        .getCarSprite())) { 
              rManager.getInstance().carArray[r].setCarSpeed(0f); 
            } 

            else if (rManager.getInstance().iceBergArray[5] 
                .getIceBergSprite().collidesWith(
                    rManager.getInstance().carArray[r] 
                        .getCarSprite())) { 
              rManager.getInstance().carArray[r].setCarSpeed(0f); 
            } 

我發現有很多教程,但似乎沒有一個參考對象的數組,而這正是我需要的。如果有人可以幫助我或提供某種快速教程,那麼將不勝感激。謝謝!!

回答