0

所以,我正在研究 AndEngine PhysicsExample 代码。我想知道这种方法的含义是什么(http://pastebin.com/Day2hciB):

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

1 回答 1

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);

这将创建一个名为 face 的精灵并将一个盒子主体附加到它上面。下一个附上一个圆圈。现在你会注意到接下来的两个是不同的。他们说 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 做同样的事情,只是用一个六边形。希望回答你的问题?如果我遗漏了什么,请告诉我。

于 2012-07-20T06:22:28.410 回答