XNA简单状态机不执行任何操作

时间:2019-02-07 15:26:16

标签: xna

我正在为我的视频游戏设计类设计一个项目,在那里我基本上必须在一些代码中找到一些错误,对其进行修复,然后使用枚举将其全部转换为状态机。我在下面粘贴了initialize,update和draw方法,如您所见,在将旧代码转换为状态机之前,我已对其进行注释。在使用所有if / else语句之前,一切工作正常,但是现在,我已经对其进行了转换,实际上它什么也没做。它会在起始位置显示站立的精灵,但是按下键绝对没有任何作用。

实例变量

    Texture2D heroineTexture, dive, duck, jump, stand;
    int yVelocity, jumpVelocity, diveVelocity;
    Rectangle player;
    enum State
    {
        Standing,
        Jumping,
        Ducking,
        Diving
    };
    State state;

    KeyboardState kb, oldkb;

初始化

        oldkb = Keyboard.GetState();
        jumpVelocity = -10;
        diveVelocity = 15;
        yVelocity = 0;
        heroineTexture = stand;
        state = State.Standing;
        player = new Rectangle(0, 430, 50, 40);
        base.Initialize();

更新

        kb = Keyboard.GetState();
        // Allows the game to exit
        if (kb.IsKeyDown(Keys.Escape))
            this.Exit();

        // TODO: Add your update logic here

        player.Y += yVelocity;

        switch (state)
        {
            case State.Standing:
                if (kb.IsKeyDown(Keys.J))
                {
                    state = State.Jumping;
                    yVelocity = jumpVelocity;
                    heroineTexture = jump;
                }
                else if (kb.IsKeyDown(Keys.Down))
                {
                    state = State.Ducking;
                    heroineTexture = duck;
                }
                break;
            case State.Jumping:
                if (kb.IsKeyDown(Keys.Down))
                {
                    state = State.Diving;
                    heroineTexture = dive;
                    yVelocity = diveVelocity;
                }
                break;
            case State.Ducking:
                if (!kb.IsKeyDown(Keys.Down))
                {
                    state = State.Standing;
                    heroineTexture = stand;
                }
                break;
        }


        if (player.Y >= 430)
        {
            state = State.Standing;
            player.Y = 430;
            yVelocity = 0;
            heroineTexture = stand;
        }

        //if (kb.IsKeyDown(Keys.J) && oldkb.IsKeyUp(Keys.J))
        //{
        //    if (!isJumping && !isDucking)
        //    {
        //        isJumping = true;
        //        yVelocity = jumpVelocity;
        //        heroineTexture = jump;
        //    }
        //}
        //else if(kb.IsKeyDown(Keys.Down))
        //{
        //    if(!isJumping)
        //    {
        //        if(isDiving)
        //        {
        //            yVelocity = diveVelocity;
        //        }
        //        else if(!isJumping && !isDiving)
        //        {
        //            isDucking = true;
        //            heroineTexture = duck;
        //        }
        //    }
        //    else
        //    {
        //        isJumping = false;
        //        heroineTexture = dive;
        //        isDiving = true;
        //    }
        //}
        //else if(!kb.IsKeyDown(Keys.Down))
        //{
        //    if(isDucking)
        //    {
        //        isDucking = false;
        //        heroineTexture = stand;
        //    }
        //}

        oldkb = kb;
        base.Update(gameTime);

1 个答案:

答案 0 :(得分:0)

通常:在设置状态的每一行上设置一个断点,开始调试,然后找到罪魁祸首。

无论如何。在注释的代码中,您对播放器进行了处理。y> = 430在处理状态之前进行了检查,但是现在您要在之后进行检查。

您基本上是在设置状态,但没有更改位置,因此,如果我猜对了,您的播放器处于站立状态(> = 430),则设置状态,然后立即(在实际更改播放器之前)播放器。仍然站立,您将状态设置为站立。

这只是从您的代码段中猜测出来,因为实际上“ if player.y> = 430”的位置实际上是在另一个时间调用的。

您需要执行以下操作:

  

player.Y + = yVelocity;

在开关/盒之前但在

之前
  

if(player.y> = 430)

相关问题