1

我想限制该区域以仅在该区域上移动精灵对象(例如,区域尺寸为 200x200)。

我会创建一个 box2D 200x200,其中精灵只能在这个区域上移动

请问你是怎么做的?

@Override
public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    final Scene scene = new Scene();
    scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

    final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegionLetterOne
            .getWidth()) / 2;
    final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegionLetterOne
            .getHeight()) / 2;
    final Sprite letterOne = new Sprite(centerX - centerX / 2, centerY
            - centerY / 2, this.mFaceTextureRegionLetterOne,
            this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                    pSceneTouchEvent.getY() - this.getHeight() / 2);
            return true;
        }


    };

    final Sprite letterTwo = new Sprite(centerX - centerX / 2, centerY,
            this.mFaceTextureRegionLetterTwo,
            this.getVertexBufferObjectManager()) {
        @Override
        public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
                final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2,
                    pSceneTouchEvent.getY() - this.getHeight() / 2);
            //int count = scene.getChildCount();
            //for(int i = 0; i < count; i++) {

            IEntity entity = scene.getChildByIndex(1);
            if (entity instanceof Sprite) {
                if (entity.getUserData().equals("sprite"))
                    if (((Sprite) entity).collidesWith(letterOne))
                        Log.v("colission", "face_box is collised on google plus -> letterTwo on letterOne");
            }
            //}
            return true;
        }
    };
    letterTwo.setUserData("sprite");

    final Sprite boxArea = new Sprite(centerX, centerY,
            this.mFaceTextureRegionBox, this.getVertexBufferObjectManager());
    letterOne.setScale(2);
    scene.attachChild(letterOne);
    scene.registerTouchArea(letterOne);


    letterTwo.setScale(2);
    scene.attachChild(letterTwo);

    scene.registerTouchArea(letterTwo);

    boxArea.setScale(2);
    scene.attachChild(boxArea);

    scene.setTouchAreaBindingOnActionDownEnabled(true);

    return scene;
}

谢谢你。

4

3 回答 3

0

我不知道 box2D,但通常你会在每次移动时检查精灵的边缘,如果它们不与区域的边缘重叠。

如果你的精灵由一个矩形表示,并且你可以移动的区域也由一个矩形表示,这可以很容易地完成。

但首先,检查 box2D API,也许它已经有一些帮助方法来简化这个任务,比如:

obect.overlaps(object)
于 2012-08-17T11:16:08.050 回答
0

使用您的代码,您似乎试图对您的精灵进行碰撞检查?!

当你试图在没有物理实体的情况下做事时,它会很复杂而且不好。因此,让我们将物理实体与您的精灵一起使用。

但请注意,不要为您的区域(包含您的精灵)创建一个实心的盒子体,让我们使用 4 个分离的体壁(左、上、右、下)来形成一个封闭的盒子;因为游戏引擎只能检查实体形状的碰撞。

以下是供参考的代码:

/**
 * @param pScene
 *      Sence of the game, get from class member
 * @param pWorld
 *      physics world of the game, get from class member
 */
public void CreateSprites(final Scene pScene, final PhysicsWorld pWorld)
{
    final FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(10, 1.1f, 0.0f);//should be placed as member of class
    final Sprite letterTwo = new Sprite(centerX - centerX / 2, centerY, 
            this.mFaceTextureRegionLetterTwo,
            this.getVertexBufferObjectManager());

    final Body letterTwoBody = PhysicsFactory.createBoxBody(pWorld, letterTwo, BodyType.DynamicBody, mFixtureDef);
    letterTwo.setUserData(letterTwoBody); // for later sprite-body attachment access

    pScene.attachChild(letterTwo);
    pWorld.registerPhysicsConnector(new PhysicsConnector(letterTwo, letterTwoBody, true, true));
}

/** Create the walls, in these boudaries sprites will move */
public void InitBoxWalls(Scene pScene, PhysicsWorld pWorld)
{
    final float WALL_MARGIN_WIDTH = 5f;
    final float WALL_MARGIN_HEIGHT = 10f;
    final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();

    mWallGround = new Rectangle(-WALL_MARGIN_WIDTH, mCameraHeight + WALL_MARGIN_HEIGHT - 2, mCameraWidth + 2*WALL_MARGIN_WIDTH, 2, vertexBufferObjectManager);
    mWallRoof = new Rectangle(-WALL_MARGIN_WIDTH, -WALL_MARGIN_HEIGHT, mCameraWidth + 2*WALL_MARGIN_WIDTH, 2, vertexBufferObjectManager);
    mWallLeft = new Rectangle(-WALL_MARGIN_WIDTH, -WALL_MARGIN_HEIGHT, 2, mCameraHeight + 2*WALL_MARGIN_HEIGHT, vertexBufferObjectManager);
    mWallRight = new Rectangle(mCameraWidth + WALL_MARGIN_WIDTH - 2, -WALL_MARGIN_HEIGHT, 2, mCameraHeight + 2*WALL_MARGIN_HEIGHT, vertexBufferObjectManager);

    PhysicsFactory.createBoxBody(pWorld, mWallGround, BodyType.StaticBody, mWallFixtureDef);
    PhysicsFactory.createBoxBody(pWorld, mWallRoof, BodyType.StaticBody, mWallFixtureDef);
    PhysicsFactory.createBoxBody(pWorld, mWallLeft, BodyType.StaticBody, mWallFixtureDef);
    PhysicsFactory.createBoxBody(pWorld, mWallRight, BodyType.StaticBody, mWallFixtureDef);

    pScene.attachChild(mWallGround);
    pScene.attachChild(mWallRoof);
    pScene.attachChild(mWallLeft);
    pScene.attachChild(mWallRight);
}

您应该做的最后一件事是在 AndEngine 示例中查找 Physics/MouseJoint 示例。

于 2012-08-23T03:26:45.533 回答
0

您应该创建一个 200X200 的 Rectangle,然后您应该使用 Box2D Physics 创建一个矩形主体。对于矩形的夹具定义,您可以设置密度、弹性和摩擦力,以便您的精灵在与矩形的边界碰撞时得到相应的处理。

要创建一个矩形,您可以参考 Andengine 的示例。

于 2012-08-22T06:20:44.680 回答