【问题标题】:Switching between Scenes in AndEngine Losses control of my physics body在 AndEngine 中的场景之间切换失去对我的物理体的控制
【发布时间】:2013-09-24 03:28:17
【问题描述】:

返回游戏场景后,我的玩家的移动出现问题。

我可以通过控件来控制我的播放器。我已经使用屏幕控制和触摸区域精灵完成了它,但是当我返回我的游戏场景时两者都失败了。两个控件仍会获得输入,但不会再移动我的播放器。

这一切都在我扩展 BaseScene 的 GameScene 中完成

这是我设置动画精灵和物理世界的方法:

    //////////////////////////////////////////////////////////////////////
    //Creates and Registers Physics World
    //////////////////////////////////////////////////////////////////////
    this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);
    this.registerUpdateHandler(this.mPhysicsWorld);

    //////////////////////////////////////////////////////////////////////
    //Creates and Adds the Animated Sprite, Sets Camera Chase Entity
    //////////////////////////////////////////////////////////////////////
    player = new AnimatedSprite(18 * 32, 43 * 32, ResourceManager.getInstance().mPlayerTextureRegion, ((DragonsReignActivity)activity).getVertexBufferObjectManager());
    camera.setChaseEntity(player);
    final FixtureDef playerFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0.5f);
    mPlayerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody, playerFixtureDef);

    //////////////////////////////////////////////////////////////////////
    //Register Physics Connector
    //////////////////////////////////////////////////////////////////////
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, mPlayerBody, true, false)
    {
            //////////////////////////////////////////////////////////////////////
            //Updates Chase Entity
            //////////////////////////////////////////////////////////////////////
            @Override
            public void onUpdate(float pSecondsElapsed)
            {
                    super.onUpdate(pSecondsElapsed);
                    camera.updateChaseEntity();
            }
    });

    physicsHandler = new PhysicsHandler(player);
    player.registerUpdateHandler(physicsHandler);
    attachChild(player);
    player.setZIndex(10);

这是我目前设置控件的方式:

public void attachControls()
{
    mDigitalOnScreenControl = new DigitalOnScreenControl(0, ((DragonsReignActivity)activity).CAMERA_HEIGHT - ResourceManager.getInstance().DPADBacking.getHeight(), this.camera, ResourceManager.getInstance().DPADBacking, ResourceManager.getInstance().DPADKnob, 0.1f, ((DragonsReignActivity)activity).getVertexBufferObjectManager(), new IOnScreenControlListener() 
    {
        @Override
        public void onControlChange(BaseOnScreenControl pBaseOnScreenControl,  float pValueX,  float pValueY) 
        {
                if (pValueY == 1){
                        // Up
                        if (playerDirection != PlayerDirection.UP)
                        {
                            player.animate(new long[]{200, 200, 200}, 0, 2, true);
                            playerDirection = PlayerDirection.UP;
                        }
                }else if (pValueY == -1){
                        // Down
                        if (playerDirection != PlayerDirection.DOWN)
                        {
                            player.animate(new long[]{200, 200, 200}, 3, 5, true);  
                            playerDirection = PlayerDirection.DOWN;
                        }
                }else if (pValueX == -1)
                {
                        // Left
                        if (playerDirection != PlayerDirection.LEFT)
                        {   
                            player.animate(new long[]{200, 200, 200}, 6, 8, true);
                            playerDirection = PlayerDirection.LEFT;
                        }
                }else if (pValueX == 1)
                {
                        // Right
                        if (playerDirection != PlayerDirection.RIGHT)
                        {

                            player.animate(new long[]{200, 200, 200}, 9, 11, true);
                            playerDirection = PlayerDirection.RIGHT;

                        }
                }else{
                    if (player.isAnimationRunning())
                    {   
                        player.stopAnimation();
                        playerDirection = PlayerDirection.NONE;
                    } 
                }

                if(player.getX() >= 576 && player.getX() <= 736 && player.getY() <= 672 && player.getY() >= 576)
                {
                    PLAYER_VELOCITY = 4;
                    Log.e("Collision Box", "You are in the Box. Player Velcocity = " + PLAYER_VELOCITY);

                }
                mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);
        }
    });
    this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
    this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
    this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
    this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
    this.mDigitalOnScreenControl.refreshControlKnobPosition();
}

最后,这就是我回到游戏场景的方式:

@Override
public void onBackKeyPressed() 
{
    SceneManager.getInstance().setScene(SceneManager.SceneType.SCENE_GAME);

    camera.setChaseEntity(GameScene.player);

}

我知道当我返回我的场景时,我的控制器仍会收到输入,但角色不会移动。

任何帮助将不胜感激

【问题讨论】:

  • 你必须对你的玩家身体施加线速度
  • @Siddharth 我将我的播放器(我的动画精灵)连接到我的 mPlayerBody(物理对象)。所以当我移动我的物理对象时,我的玩家也会随之移动。我这样做是为了解决我无法正常工作的 TMX 碰撞,所以我在 Collidable TMX Tiles 顶部的屏幕上添加了物理对象。所以行 mPlayerBody.setLinearVelocity(pValueX * PLAYER_VELOCITY, pValueY * PLAYER_VELOCITY);确实可以正确移动玩家,直到我更改场景并回到我的游戏场景
  • 基本的经验法则是,如果您使用的是物理世界,那么您只能使用与物理相关的任务来移动对象,否则您在屏幕上看不到任何效果。这已经发生在你身上。
  • @Siddharth 感谢您的回复,但最初移动对象不是问题。当我开始我的游戏场景时,一切都正确加载,我的角色按预期移动,但是当我改变场景并回到我的游戏场景时,我无法再移动玩家。控制器仍然可以正常输入,但它不会再移动玩家。
  • 抱歉最后两个 cmets,我没有看到你的速度声明。如果您使用的是物理世界,则不需要物理处理程序。此外,当您当时更改场景时,您必须在那时清除与物理相关的东西,以便下次您来创建所有新对象。

标签: controller box2d andengine game-physics


【解决方案1】:
  • 测试你的身体是否在更新位置。

    1. 如果 body 正在更新,而 sprite 没有响应您的输入,那么它与您的 sprite 不一致。
    2. 我怀疑你为什么将“registerUpdateHandler”放到精灵中。因为你已经通过世界更新你的身体。然后精灵也会自动根据身体更新。
    3. 如果不是,可能是世界没有更新。

【讨论】:

  • 感谢@Rama 的回复。 1. 身体和精灵都正确更新,直到我回到我的场景。 2. Sprite 正在获取“registerUpdateHandler”,因为在上面的代码块中,我使用物理连接器来链接玩家和 mPlayerBody。 3. 我更倾向于认为这是我的世界的一个问题,但我不知道我将如何开始解决这个问题。主要问题是那里的每个示例都在他们的主要游戏活动中运行他们的玩家,因为我已经创建了游戏以通过一个活动从场景运行。我找不到解决方案。谢谢,弗罗斯特
  • 尝试将固定世界改为普通物理世界
  • 更改为普通的物理世界不会改变任何事情。我认为问题是当我切换场景并返回 CreateScene 时永远不会被重新调用,所以我的物理内容不会得到更新。你知道解决这个问题的方法吗?
猜你喜欢
  • 2020-04-13
  • 2016-09-09
  • 2020-03-23
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多