模拟蜈蚣有限状态机

时间:2013-11-17 21:26:52

标签: algorithm actionscript-3 fsm

我正在尝试编写一个蜈蚣游戏克隆,并且我编写了一个基本上是圆形的节点类。 我正在尝试编写一个FSM来模拟节点移动并用砖块或屏幕弹跳,但问题是FSM与其他条件冲突。我该如何解决?例如,其中一个节点可以是x <0但是它们具有y> 0。 800.

public function gameLoop(event:Event):void
    {
        for (var i:int = 0; i < m_nodes.length; i++)
        {

            if (m_nodes[i].x > 750)
            {
                m_nodes[i].current_dir = Node.Direction_RIGHT;

            }
            else  if (m_nodes[i].x < 10)
            {

                m_nodes[i].current_dir = Node.Direction_UP;

            }

            if ( m_nodes[i].y < 0 )
            {
                m_nodes[i].current_dir = Node.Direction_RIGHT;

            }


            else if ( m_nodes[i].y > 590 )
            {
                m_nodes[i].current_dir =Node.Direction_LEFT;
            }




            if (m_nodes[i].hitTestObject(m_bricks[i]))
            {

                //m_nodes[i].current_dir = Node.Direction_RIGHT; 

            }

            if (m_nodes[i].current_dir == Node.Direction_LEFT)
            {
                m_nodes[i].vx = -5;
                m_nodes[i].vy = 0;
            }

            if (m_nodes[i].current_dir == Node.Direction_RIGHT)
            {

                m_nodes[i].vx = 0;
                m_nodes[i].vy = 5;
            }

             if (m_nodes[i].current_dir == Node.Direction_UP)
            {

                m_nodes[i].vx = 0;
                m_nodes[i].vy = -5;
            }

            m_nodes[i].x += m_nodes[i].vx;
            m_nodes[i].y += m_nodes[i].vy;


        }

    }

更新:新的更新代码,修复逻辑,但碰撞检测失败,状态机有冲突状态。

for (var i:int = 0; i < m_nodes.length; i++)
        {

            if (m_nodes[i].x > 750)
            {
                m_nodes[i].current_dir = Node.Direction_DOWN;

            }
            else if (m_nodes[i].x < 40 && m_nodes[i].y < 600)
            {
                if (!m_nodes[i].current_dir == Node.Direction_RIGHT)
                {
                    m_nodes[i].current_dir = Node.Direction_UP;
                    trace("dir up");
                }

            }


            if (m_nodes[i].hitTestObject(m_bricks[i]) )
            {
                trace("hit");
                m_nodes[i].vx = 0;
                m_nodes[i].vy = 5;
            }



            if (m_nodes[i].y < 40 && m_nodes[i].x < 40)
            {
                m_nodes[i].current_dir = Node.Direction_RIGHT;

            }

            else if (m_nodes[i].y > 560 && m_nodes[i].x > 40)
            {
                m_nodes[i].current_dir = Node.Direction_LEFT;
            }

            if (m_nodes[i].current_dir == Node.Direction_LEFT)
            {
                m_nodes[i].vx = -5;
                m_nodes[i].vy = 0;
            }

            else if (m_nodes[i].current_dir == Node.Direction_DOWN)
            {

                m_nodes[i].vx = 0;
                m_nodes[i].vy = 5;
            }
            else if (m_nodes[i].current_dir == Node.Direction_RIGHT)
            {

                m_nodes[i].vx = 5;
                m_nodes[i].vy = 0;
            }

            if (m_nodes[i].current_dir == Node.Direction_UP)
            {

                m_nodes[i].vx = 0;
                m_nodes[i].vy = -5;
            }

            m_nodes[i].x += m_nodes[i].vx;
            m_nodes[i].y += m_nodes[i].vy;


        }

0 个答案:

没有答案