简体   繁体   English

Andengine Box2d用另一个替换精灵

[英]Andengine Box2d replace sprite with another

I've been using AndEngine for a year now and feel it's time to move on to some physics. 我已经使用AndEngine一年了,觉得现在该继续学习一些物理了。 I've followed the AndEngine Physics Example to start a Roll-a-Ball game where a ball is rolled around the screen using the accelerometer to guide it into a hole. 我已经按照AndEngine Physics实例开始了“滚球”游戏,其中使用加速度计将球绕着屏幕滚动,将球引导到一个洞中。 I got my ball rolling OK but can't make it drop into the hole. 我的球滚动正常,但无法使其掉入洞中。 I've Googled and tried everything but the best I can get is the ball collides with the hole and either bounces off it or rolls over it. 我已经用Google搜索并尝试了所有方法,但我能得到的最好的结果是,球与球洞碰撞并弹起或翻滚。

Here's some code. 这是一些代码。

This is my ball. 这是我的球。

    ball = new AnimatedSprite(pX, pY,this.mBallFaceTextureRegion,this.getVertexBufferObjectManager());
    bodyBall = PhysicsFactory.createCircleBody(this.mPhysicsWorld,ball,BodyType.DynamicBody, FIXTURE_DEF);
    bodyBall.setUserData("Ball");
    ball.animate(200);

    this.mScene.attachChild(ball);
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, bodyBall, true, true));

Thia is my hole. 蒂亚是我的洞。

    hole = new Sprite(pX, pY, this.hHoleFaceTextureRegion, this.getVertexBufferObjectManager());
    bodyHole = PhysicsFactory.createCircleBody(this.mPhysicsWorld, hole, BodyType.StaticBody, FIXTURE_DEF);
    bodyHole.setUserData("Hole"); // ID for hole
    this.mScene.attachChild(hole);
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(hole, bodyHole, false, false));

This is my ContactListener inside Scene onCreateScene() with some of code I've tried. 这是我在Scene onCreateScene()中的ContactListener,其中包含我尝试过的一些代码。

    this.mPhysicsWorld.setContactListener(new ContactListener() {
     @Override
    public void beginContact(final Contact pContact) {
        final Body BodyA = pContact.getFixtureA().getBody();
        final Body BodyB = pContact.getFixtureB().getBody();

        if(BodyA.getUserData() == "Ball" && BodyB.getUserData() == "Hole"){
            ball.setPosition(hole);
            // BodyA.setType(BodyType.StaticBody);  // bodyBall
            //mPhysicsWorld.destroyBody(BodyB); // bodyHole
        }else if(BodyA.getUserData() == "Hole" && BodyB.getUserData() == "Ball"){
            ball.setPosition(hole);
            // BodyB.setType(BodyType.StaticBody);  // bodyBall
            // hole.dispose(); // bodyHole
        }    
}

Could someone please explain how to swap the hole for the ball or some other way to simulate the ball dropping into the hole. 有人可以解释一下如何将孔替换为球或通过其他方式模拟球掉入孔中。 Thanks 谢谢

SOLVED. 解决了。

Found the solution at [Removing Body completely 1 ]. 在[完全拆卸机身1 ]处找到了解决方案。 You must delete the hole and reset the ball in a separate thread. 您必须删除孔并将球重置为单独的螺纹。
I created a new method SwapHoleAndBall() and called it from within beginContact. 我创建了一个新方法SwapHoleAndBall()并从beginContact内对其进行了调用。

Here's the modified code. 这是修改后的代码。

    public void beginContact(final Contact pContact) {
        final Body BodyA = pContact.getFixtureA().getBody();
        final Body BodyB = pContact.getFixtureB().getBody();

        if(BodyA.getUserData() == "Ball" && BodyB.getUserData() == "Hole"){
            SwapHoleAndBall();
            ......

And the swap method. 和交换方法。

    public void SwapHoleAndBall() {
    final PhysicsConnector physicsConnector =
            mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(hole);

        mEngine.runOnUpdateThread(new Runnable() 
        {
            @Override
            public void run() 
            {
                if (physicsConnector != null)
                {
                    bodyBall.setTransform(bodyHole.getPosition(), 0);
                    mPhysicsWorld.unregisterPhysicsConnector(physicsConnector);
                    bodyHole.setActive(false);
                    mPhysicsWorld.destroyBody(bodyHole);
                    mScene.detachChild(hole);
                    bodyBall.setType(BodyType.StaticBody);
                }
            }
        });
    }

Hope this will help someone else. 希望这会帮助别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM