Time between attacks

Non-platform specific questions.

Time between attacks

Postby Freddy » Sun Jul 29, 2007 8:39 pm

This is the ai script I used for my opponent, [code]if (abs(Player.xscreen - xscreen) < 200)
{
if (Player.xscreen > xscreen + 100)
{
if (animindex < 3)
{
ChangeAnimation("Event Actor", "walkright", FORWARD);
x += 5;
}
}
else if (Player.xscreen > xscreen)
{
if (animindex != 5)
{
ChangeAnimation("Event Actor", "attackright", FORWARD);
}
}
else if (Player.xscreen < xscreen - 100)
{
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;
}
}
[/code] But the problem is, the bad actor punches way too fast. Can I set a certain speed to it?
User avatar
Freddy
 
Posts: 548
Joined: Sat Jun 02, 2007 3:42 pm
Location: Why do you want to know?
Score: 14 Give a positive score

Postby DocRabbit » Mon Jul 30, 2007 1:50 am

Code: Select all
if (abs(Player.xscreen - xscreen) < 200)
     {
      if (Player.xscreen > xscreen + 100)
             {
                    if (animindex < 3)
                            {
                             ChangeAnimation("Event Actor", "walkright", FORWARD);
                             x += 5;
                            }
            }
      else if (Player.xscreen > xscreen)
           {
                   if (animindex != 5)
                           {
                            ChangeAnimation("Event Actor", "attackright", FORWARD);
                           }
           }
     else if (Player.xscreen < xscreen - 100)
           {
                  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;
    }
}


Trying to get a handle on your code here, I am assuming this is the code from the draw actor event of the bad guy? I also assume the attackleft/attackright animation is what you are referring to as punching..
User avatar
DocRabbit
 
Posts: 114
Joined: Fri Oct 27, 2006 2:56 am
Score: 10 Give a positive score

Postby DilloDude » Mon Jul 30, 2007 2:41 am

Create an actor integer called time. Where you have
Code: Select all
if (animindex != 5)
                           {
                            ChangeAnimation("Event Actor", "attackright", FORWARD);
                           }
and its eqivalent for attackleft, change it to
Code: Select all
if (time == 10)//adjust this value
    if (animindex != 5)
    {
        ChangeAnimation("Event Actor", "attackright", FORWARD);
        time = 0;
    }
}
else
{
time ++;
}
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Freddy » Mon Jul 30, 2007 3:50 pm

I tried the code, but it said I needed a ; on line 41, 57 and line 46, and that there was a misplaced break statement on line 53 and 56. So I'm pretty much lost. I also read the post reply you made when you were talking to acreamer (I thing I spelled the name right) :? and I only understand bits and pieces of this code. Could you help me out?
User avatar
Freddy
 
Posts: 548
Joined: Sat Jun 02, 2007 3:42 pm
Location: Why do you want to know?
Score: 14 Give a positive score

Postby DilloDude » Wed Aug 01, 2007 8:34 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Freddy » Thu Aug 02, 2007 5:10 pm

Wow, I never thought that it could be so simple! :D Like you said, that it was made up of alot of little codes. Thanks so much!! :D :D
Hola
User avatar
Freddy
 
Posts: 548
Joined: Sat Jun 02, 2007 3:42 pm
Location: Why do you want to know?
Score: 14 Give a positive score

Postby Freddy » Thu Aug 02, 2007 6:02 pm

Everything works fine, except I cant adjust the distance before the player attacks :( , but the code worked! :D :D :D :D I was so happy to see that it took a break between punches! :D :D
Hola
User avatar
Freddy
 
Posts: 548
Joined: Sat Jun 02, 2007 3:42 pm
Location: Why do you want to know?
Score: 14 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest