2012-07-01 79 views
0

所以,我正在研究AndEngine PhysicsExample代碼。我想知道這是什麼方法的含義(http://pastebin.com/Day2hciB):AndEngine物理示例

private void addFace(final float pX, final float pY) { 
     this.mFaceCount++; 
     Debug.d("Faces: " + this.mFaceCount); 

     final AnimatedSprite face; 
     final Body body; 

     if(this.mFaceCount % 4 == 0) { 
      face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager()); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
     } else if (this.mFaceCount % 4 == 1) { 
      face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager()); 
      body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
     } else if (this.mFaceCount % 4 == 2) { 
      face = new AnimatedSprite(pX, pY, this.mTriangleFaceTextureRegion, this.getVertexBufferObjectManager()); 
      body = PhysicsExample.createTriangleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
     } else { 
      face = new AnimatedSprite(pX, pY, this.mHexagonFaceTextureRegion, this.getVertexBufferObjectManager()); 
      body = PhysicsExample.createHexagonBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
     } 

     face.animate(200); 

     this.mScene.attachChild(face); 
     this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true)); 
    } 
+4

請把代碼放在這裏,而不是在第三方網站上,並解釋你不明白什麼請參閱[如何提問](http://stackoverflow.com/questions/how-to-ask) – Mark

回答

5
private void addFace(final float pX, final float pY) { 
      this.mFaceCount++; 
      Debug.d("Faces: " + this.mFaceCount); 

      final AnimatedSprite face; 
      final Body body; 

      if(this.mFaceCount % 4 == 0) { 
        face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager()); 
        body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
      } else if (this.mFaceCount % 4 == 1) { 
        face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager()); 
        body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
      } else if (this.mFaceCount % 4 == 2) { 
        face = new AnimatedSprite(pX, pY, this.mTriangleFaceTextureRegion, this.getVertexBufferObjectManager()); 
        body = PhysicsExample.createTriangleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
      } else { 
        face = new AnimatedSprite(pX, pY, this.mHexagonFaceTextureRegion, this.getVertexBufferObjectManager()); 
        body = PhysicsExample.createHexagonBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
      } 

      face.animate(200); 

      this.mScene.attachChild(face); 
      this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true)); 
    } 

這段代碼的作用是設置一個機構,根據其形狀的精靈。每次添加面孔時,mFaceCount都會爲其自身添加1。這行的作用:

if(this.mFaceCount % 4 == 0) 

是檢查以查看是否當除以4的餘數是等於0(和1,2,3爲其他)。所有這些都告訴它要添加到場景中的哪個精靈。你會注意到,你首先添加一個正方形,然後是一個圓,然後是三角形,然後是六角形。

真正的代碼是在這裏這些行:

face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager()); 
        body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 

這將創建一個名爲臉精靈並附加一個箱體它。下一個附加一個圓圈。現在你會注意到接下來的兩個是不同的。他們說PhysicsExample.create而不是PhysicsFactory.create。 PhysicsExample是活動,所以他們在PhysicsExample中調用一個方法,而不是從PhysicsFactory中調用。 createTriangleBody實際上調用此方法(在稍後的代碼):

private static Body createTriangleBody(final PhysicsWorld pPhysicsWorld, final IAreaShape pAreaShape, final BodyType pBodyType, final FixtureDef pFixtureDef) { 
    /* Remember that the vertices are relative to the center-coordinates of the Shape. */ 
    final float halfWidth = pAreaShape.getWidthScaled() * 0.5f/PIXEL_TO_METER_RATIO_DEFAULT; 
    final float halfHeight = pAreaShape.getHeightScaled() * 0.5f/PIXEL_TO_METER_RATIO_DEFAULT; 

    final float top = -halfHeight; 
    final float bottom = halfHeight; 
    final float left = -halfHeight; 
    final float centerX = 0; 
    final float right = halfWidth; 

    final Vector2[] vertices = { 
      new Vector2(centerX, top), 
      new Vector2(right, bottom), 
      new Vector2(left, bottom) 
    }; 

    return PhysicsFactory.createPolygonBody(pPhysicsWorld, pAreaShape, vertices, pBodyType, pFixtureDef); 
} 

這就造成各地精靈三角形(不要問我怎麼不明白他在這裏所使用的數學,但我複製它。完全一樣,它適用於大致等邊三角形,整個頂點的東西需要一些玩,而我沒有獲得矢量!!)。 createHexagonMethod只是用六角形做同樣的事情。回答你的問題,我希望?如果我遺漏了任何東西,請告訴我。