Sorry for the late reply, I've been rather busy lately.
The first line is an if statement:
- Code: Select all
Code:
if (abs(Player.xscreen - xscreen) < 200)
this finds the distance between Player and the event actor on the x axis, and compares it with 200. If it is less than 200 (ie, the event actor is less than 200 pixels away) then it will proceed to the next bit.
The next section is inside the if statement, so it will only get executed if the first test was true.
- Code: Select all
if (Player.xscreen > xscreen + 100)
if the player is further away to the right than 100 pixels, you want to walk toward him. So we chek that we are not already walking that direction and that we are not attacking:
- Code: Select all
if (animindex < 3)
Because of the order of animations, this checks if the animation is walking left, or if it is stopped. If it is eaither of these cases, we want to change it to walkright.
- Code: Select all
ChangeAnimation("Event Actor", "walkright", FORWARD);
The next bit if moving right, using
- Code: Select all
x += 5
or some other value. This increases the x value.
NOTE in the original code, this is inside the if statement with the animation change. This is a mistake (my bad) it should be just after it:
- Code: Select all
if (animindex < 3)
{
ChangeAnimation("Event Actor", "walkright", FORWARD);
}
x += 5;
and the same for walk left.
Next we have an else if statement. The else is executed when the previous if (if (Player.xscreen > xscreen + 100)) was not true, in this case, the player is closer than 100 pixels or is to the left. We use an else if because we want to make another test
- Code: Select all
else if (Player.xscreen > xscreen)
Because the previous if was false, we use this if to find if that is because the player is closer or if it is in the other direction. In this case, the player is close enough to attack:
- Code: Select all
if (animindex != 5)
{
ChangeAnimation("Event Actor", "attackright", FORWARD);
}
This is similar to above, except this time we want to rule out more animations: we change to attacking only if it is not already the current animation. The != means 'is not equal to'.
Now, another else if. This time, we have ruled out the player being on the right side so he must be to our left. This next check finds how far to the left. When this is true, he is far enough away to walk left.
- Code: Select all
{
if (animindex < 2 || animindex == 4)
{
ChangeAnimation("Event Actor", "walkleft", FORWARD);
}
x -= 5;
}
This code is similar to the one above (remembering to relocate the x -= 5 to the next set of braces, as above). Only this time, our animindex isn't in such a nice order, so we have to make two checks: animindex < 2 to see if we are stopped and animindex == 4 to see if we are walking right (the || means 'or'). If either of these things are happening, we change our animation to waling left.
Finally, we ad an else statement. The is executed if all the above things are false. In this case, if the player is close enough on our left side (or, you'll notice, exactly on top of us). In this case we attack to the left, usingmuch the same check as attacking right, above:
- Code: Select all
if (animindex != 4)
{
ChangeAnimation("Event Actor", "attackleft", FORWARD);
}
Now, we arrive at another else statement, this time after the original
- Code: Select all
if (abs(Player.xscreen - xscreen) < 200)
This is the code that executes if the player is too far away for us to notice. We are going to use a switch statement. This is a special type of conditional control statement. You put in an integer variable (in this case, animindex). The point at which we then enter the code depends upon our case statements. Once we enter, we keep going untill we find a 'break' statement. To find more about switches, do a forum search (it has been covered before).
- Code: Select all
switch(animindex)
{
case 3: case 5:
ChangeAnimation("Event Actor", "stopright", FORWARD);
break;
case 2: case 4:
ChangeAnimation("Event Actor", "stopleft", FORWARD);
break;
}
This means that if animindex is 3 or 5, (our values for attacking right and walking right) we change animation to stopright. On the other hand, if our animindex is 2 or 4 (our values for attacking and walking left) we change our animation to stopleft. In the case that we are already stopped, nothing will happen.
This brings us to the end of our lovely long script, I hope that helps you a bit. Like most scripts, it is long, but consists of fairly simple elements. It is a lot nicer to write than to read. The animation finish events that you should have added will stop the attacking animation when it finishes (otherwise, you will go on attacking forever). Of course, as soon as the animation finishes, you are then able to attack again, meaning you can attack really rapidly. The modifacation I made was to add a delay to it. I just noticed I missed out a {. It should be
- Code: Select all
if (time == 10)//adjust this value
{
if (animindex != 5)
{
ChangeAnimation("Event Actor", "attackright", FORWARD);
time = 0;
}
}
else
{
time ++;
}
Try it with that adjustment, and see if it works. If not, I may be able to take a look at it.