Here's a simple code. You may want to customize it somewhat.
Make sure the animations on the enemy are in this order: stopleft, stopright, walkleft, walkright, attackleft, attackright. It doesn't matter, except that it will effect the animindex values. So if the anims are in a different order, you will need to modify some values.
First, set up the basic collisions on the enemy actor. Then, add a draw actor event->script editor:
- Code: Select all
if (abs(player.xscreen - xscreen) < 200)//range of enemy
{
if (player.xscreen > xscreen + 20)//adjust the 20, this is distance before attacking
{
if (animindex < 3)
{
ChangeAnimation("Event Actor", "WalkRight", FORWARD);
x += 5;
}
}
else if (player.xscreen > xscreen)
{
if (animindex != 5)
{
ChageAnimation("Event Actor", "AttackRight", FORWARD);
}
}
else if (player.xscreen < xscreen - 20)
{
if (animindex < 2 || animindex == 4)
{
ChangeAnimation("Event Actor", "WalkLeft", FORWARD);
x -= 5;
}
}
else
{
if (animindex != 4)
{
ChangeAnimation("Event Actor", AttackLeft", FORWARD);
}
}
}
else
{
switch(animindex)
{
case 3: case 5:
ChangeAnimation("Event Actor", "StopRight", FORWARD);
break;
case 2: case 4:
ChangeAnimation("Event Actor", StopLeft", FORWARD);
break;
}
}
on animation finish->AttackRight->ChangeAnimation->StopRight->FORWARD.
on animation finish->AttackLeft->ChangeAnimation->StopLeft->FORWARD.