2014-07-22 43 views
1

表達:m_bodyCount < m_bodyCapacityBox2D的崩潰(表達式:m_bodyCount <m_bodyCapacity)

即我發現了錯誤。當我得到這個錯誤時整個遊戲崩潰。現在,事情就是這樣:我只在某些方面得到錯誤,我試圖摧毀,更具體地說,是在比賽期間製造的屍體。我的意思是在遊戲開始時,我加載關卡併爲每個關卡創建所有物體。但是,有些項目是在遊戲運行時製作的,例如子彈,大炮和硬幣。我可以銷燬裝入關卡時創建的任何物體,但是當我嘗試銷燬之後創建的物體時,我會得到上述錯誤。

下面是一個例子。我生成一個炮彈與我的炮: Cannon.class

if(reloadProgress >= RELOAD_TIME) { 
      CannonBall cannonBall = new CannonBall(); 
      cannonBall.setXDirection(xDirection); 
      cannonBall.setYDirection(yDirection); 
      cannonBall.position.set(cannonBallPos); 
      Box2DHelper.makeFixture(cannonBall, BodyType.KinematicBody, origin, WorldController.b2world, 
        true, false); 
      Level.cannonBalls.add(cannonBall); 
      reloadProgress -= RELOAD_TIME; 
     } else { 
      reloadProgress += deltaTime; 
     } 

凡Box2DHelper.makeFixture()是:

public static void makeFixture(GameObject object, BodyType type, 
      Vector2 origin, World b2world, boolean addUserData, boolean isMonster) { 
     BodyDef bodyDef = new BodyDef(); 
     bodyDef.type = type; 
     bodyDef.position.set(object.position); 
     Body body = b2world.createBody(bodyDef); 
     if(addUserData) 
      body.setUserData(object); 
     object.body = body; 
     PolygonShape polygonShape = new PolygonShape(); 
     origin.x = object.bounds.width/2.0f; 
     origin.y = object.bounds.height/2.0f; 
     polygonShape.setAsBox(object.bounds.width/2.0f, 
       object.bounds.height/2.0f, origin, 0); 
     FixtureDef fixtureDef = new FixtureDef(); 
     fixtureDef.shape = polygonShape; 
     if(isMonster) fixtureDef.filter.groupIndex = Constants.GROUP_MONSTER; 
     body.createFixture(fixtureDef); 
     polygonShape.dispose(); 
    } 

所以,現在我已經創建了我的炮彈,並設置它的身體。現在,10秒後,如果還沒有打什麼,我毀了它:

CannonBall.class

if (existanceDuration >= CANNON_BALL_EXISTANCE_DURATION) { 
      //Destroy cannon ball 
      CollisionHandler.bodiesToRemoveList.add(this.body); 
     } 

現在,我已經添加了人體進入名單,我必須將其刪除: CollisionHandler.class:

public static void removeSpecifiedBodies() { 
     Iterator<Body> i = bodiesToRemoveList.iterator(); 
     while (i.hasNext()) { 
      Body desBod = i.next(); 
      removeObjectFromList(desBod); //This is just to remove the object as well 

      WorldController.b2world.destroyBody(desBod); 

      if (perCharHitAction == true) { 
       performCharacterDeathAction(Level.character); 
       perCharHitAction = false; 
      } 
      i.remove(); 
     } 
     bodiesToRemoveList.clear(); 
    } 

但現在,我需要實際調用此方法。我這樣做,我的主要更新方法,b2world.step右後(): WorldController.class:

public void update(float deltaTime) { 
     ... 
     b2world.step(deltaTime, 8, 3); 
     if (CollisionHandler.bodiesToRemoveList.size() > 0) 
      CollisionHandler.removeSpecifiedBodies(); 

現在,就像我前面所說的,當我在破壞的裝載人造物體這種方法工作正常級別,但在後來創建的對象上,它不會。

爲什麼我的遊戲在試圖摧毀後來在我的遊戲中創建的box2d物體後繼續崩潰?

編輯:在後面的比賽創造機構當在b2world

private void initPhysics() { 
     if (b2world != null) 
      b2world.dispose(); 
     b2world = new World(new Vector2(0, -9.81f), true); 
     Vector2 origin = new Vector2(); 
     b2world.setContactListener(new CollisionHandler()); 

     // Create Box2D Fixtures 
     for (Grass grass : Level.grasses) 
      Box2DHelper.makeEdgeChain(grass, BodyType.StaticBody, origin, 
        b2world, true); 

     // Bricks 
     for (Brick brick : Level.bricks) 
      // Box2DHelper.makeFixture(brick, BodyType.KinematicBody, origin, 
      // b2world, true); 
      Box2DHelper.makeEdgeChain(brick, BodyType.StaticBody, origin, 
        b2world, true); 

     // Item Boxes 
     for (ItemBox box : Level.itemBoxes) 
      Box2DHelper.makeFixture(box, BodyType.KinematicBody, origin, 
        b2world, true, false); 

     //Enemies 
     for (Goomba goomba : Level.goombas) 
      Box2DHelper.makeFixture(goomba, BodyType.DynamicBody, origin, 
        b2world, true, true); 

     for (Koopa koopa : Level.koopas) 
      Box2DHelper.makeFixture(koopa, BodyType.DynamicBody, origin, 
        b2world, true, true); 

     // Cannons 
     for (Cannon cannon : Level.cannons) 
      Box2DHelper.makeFixture(cannon, BodyType.KinematicBody, origin, 
        b2world, true, true); 


     // Character 
     BodyDef bodyDef = new BodyDef(); 
     bodyDef.type = BodyType.DynamicBody; 
     bodyDef.position.set(Level.character.position); 
     Body body = b2world.createBody(bodyDef); 
     body.setUserData(Level.character); 
     Level.character.body = body; 

     CircleShape polygonShapeHead = new CircleShape(); 
     origin.x = Level.character.circleBoundOrigin.x * 2.0f; 
     origin.y = Level.character.circleBoundOrigin.y * 3.0f; 
     // polygonShapeHead.setAsBox(level.character.circleBoundOrigin.x, 
     // level.character.circleBoundOrigin.y, origin, 0); 
     polygonShapeHead.setPosition(origin); 
     polygonShapeHead.setRadius(Level.character.circleBoundOrigin.x); 
     FixtureDef fixtureDefHead = new FixtureDef(); 
     fixtureDefHead.shape = polygonShapeHead; 
     fixtureDefHead.friction = Level.character.friction.x; 
     body.createFixture(fixtureDefHead); 

     polygonShapeHead.dispose(); 

     PolygonShape polygonShapeBod = new PolygonShape(); 
     origin = Level.character.rectBoundOrigin; 
     polygonShapeBod.setAsBox(Level.character.rectBoundOrigin.x, 
       Level.character.rectBoundOrigin.y, origin, 0); 
     FixtureDef fixtureDefBod = new FixtureDef(); 
     fixtureDefBod.shape = polygonShapeBod; 
     fixtureDefBod.friction = Level.character.friction.x; 
     body.createFixture(fixtureDefBod); 

     polygonShapeBod.dispose(); 
    } 

也許這個問題:我把這個方法的水平加載用於創建對象的身體後,對不對?

+0

我會懷疑加載關卡時產生的物體和後來做出的物體之間做了什麼不同的事情?加載關卡時所做的屍體是否都使用完全相同的程序? – iforce2d

+0

對不起,遲到的迴應。我很確定我正在以同樣的方式製作它們。我只是調用Box2DHandler函數來創建它們。讓我添加一個編輯,顯示如何在加載關卡時創建主體。 – user2082169

+0

我終於解決了這個問題。我沒有將身體添加到ContactListener的銷燬列表中,而是向對象添加了一個新的布爾值,並在我想銷燬它們時將其設置爲true。然後,在對象更新方法中,我將它添加到銷燬列表中。這似乎修復了它。我認爲這可能是一個問題,要麼從對象獲取主體,要麼將其添加到聯繫人偵聽器中的銷燬列表中。 – user2082169

回答

3

我有同樣的錯誤 - 我想通了,它發生如果你多次摧毀同一個身體。

在我的情況下,我把身體放入聯繫人偵聽器的列表中,但是我沒有確定它只在列表中出現過一次。

這也解釋了爲什麼你的問題被解決了,一旦你使用了一個布爾標誌,這也確保了同一個主體只被銷燬一次。

簡而言之:如果您嘗試一遍又一遍地摧毀同一個身體,則會出現此錯誤。