Cloning Centipede Movement State Machine

Started by
1 comment, last by Nypyren 7 years ago

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;

}
}

Game Programming is the process of converting dead pictures to live ones .
Advertisement

here is a video that shows the problem

Game Programming is the process of converting dead pictures to live ones .
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
}

This topic is closed to new replies.

Advertisement