Sign in to follow this  

Cloning Centipede Movement State Machine

Recommended Posts

I'm cloning centipede, I'm having a problem with state machine I would like to move left then down the move right, then move down, then move left

private function moveIt(e:TimerEvent):void
{
for (var i:int = 1; i < 5; ++i)
{

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

}

else if (m_nodes[i].y > 560 && m_nodes[i].x > 40)
{
m_nodes[i].current_dir = Element.DOWN;

}

else if (m_nodes[i].x > 700 - 10)
{
m_nodes[i].current_dir = Element.DOWN;

}



if (m_nodes[i].current_dir == Element.RIGHT)
{

m_nodes[i].vx = 5;
m_nodes[i].vy = 0;
}
else if (m_nodes[i].current_dir == Element.LEFT)
{
m_nodes[i].vx = -5;
m_nodes[i].vy = 0;
}

else if (m_nodes[i].current_dir == Element.DOWN)
{

m_nodes[i].current_dir = Element.RIGHT;

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;

}
}
 

 

Share this post


Link to post
Share on other sites
Posted (edited)
The biggest problems are:

- Your left wall collision check needs to only happen if you're moving left. It's happening when you're moving right, which is why they get stuck in the down direction.

- You have no code which assigns them to the left direction.


The state machine should look like this:
 
if (moving down && previous direction == right)
{
  move left
}
else if (moving down && previous direction == left)
{
  move right
}

if (moving left && left is blocked)
{
  move down
  previous direction = left
}
else if (moving right && right is blocked)
{
  move down
  previous direction = right
}
Edited by Nypyren

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this