Jump to content
  • Advertisement
Sign in to follow this  
AhmedSaleh

Cloning Centipede Movement State Machine

This topic is 611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!