Make enemy run away and shoot at me

please help

if(this.alive && this.hit==false && (_root.health<=50))
{
this.gotoAndStop(“walk”);
this._x -= 5;

}  that makes it run away on the x scale when the hp is low enough

but how to make make it only go to a certain distance then face me and

randomchance of gotoAndStop(“fire”)

dont rly needa answer this part jsut the above part. this par tmight not be possible anyways lol

after it shoots me i want it to then reactivate the the previous funtion

the previous function was to follow hero

how would i make it do this firing thing then activate what it did before health was 50 again

I’ve never implemented very complex behaviour in an enemy. But I’ve started toying with the idea of a maintaining an array of states, and an object of triggers. The triggers would push or pop states from the array under certain conditions, and activate or deactivate triggers.

In this case, for example, there would be a trigger that is activated when the health drops below 50. It then pushes two states onto the array, the state of attacking, and the state of running away. Then it activates a trigger that is triggered if it exceeds a certain distance. That trigger pops the running state from the stack and activates a timer-based trigger. The timer based trigger then pops the attacking state from the stack, which means it returns to whatever state it was before this started.

I have not completely decided whether I’d want to maintain the triggers as their own list, or attach a list of triggers to each possible state. The former allows some more flexibility, but the latter eliminates some complexity.

Of course, this system is complex but nicely adaptable. If you want to keep it simple, the part you should still take to heart is you should break down the enemy’s behaviour into two things: states and the conditions under which it changes states. You probably don’t need an array of states, a single variable will probably do. Then use a switch statement to choose different behaviour depending upon the state.

im not understanding how to do all what your saying lol i just needed a flash code

i tried this but it wont work

if(this.alive && this.hit==false && (_root.health<=50))
{
startPos = this._x
this.gotoAndStop(“walk”);
this._x -= 5;

if(this._x == startPos + 50) {
this._x +=5
}
}

Ideally you should use states. Your enemy would have a list [COLOR=blue][COLOR=blue][COLOR=blue][COLOR=blue]of[/COLOR][/COLOR][/COLOR][/COLOR] states, which one with a certain behavior, triggers would set the enemy’s current state. For instance:


onEnterFrame = function() {
 distance = player._x - evilGuy._x;
 switch(enemyState){
  case 'fire':
   //Enemy fires, include here all code related to firing.
   enemyState = 'follow';
  break;
 
  case 'flee':
   if (distance <= safeDistance) {
    evilGuy._x -= speed * distance / Math.abs(distance);
   } else {
    enemyState = 'fire';
   }
  break;
 
  case 'follow':
   evilGuy._x += speed * distance / Math.abs(distance);
  break;
  }
}

First a small intro on the switch clause as many people doesn’t use it. It checks for the value [COLOR=blue][COLOR=blue]of[/COLOR][/COLOR] the variable between brackets and it has a list [COLOR=blue][COLOR=blue]of[/COLOR][/COLOR] possible values (cases) and what to do for each [COLOR=blue][COLOR=blue]of[/COLOR][/COLOR] those values. everything between ‘case <casename>:’ and ‘break;’ is executed in case the value [COLOR=blue][COLOR=blue]of[/COLOR][/COLOR] that variable matches <casename>.

Now, to our states… By default, enemy’s state would be ‘follow’, when the trigger is activated (let’s say, it gets hit), it would be set to ‘flee’. In flee mode, as you can see, it checks for the distance between him and the player, if it’s lower than the safeDistance variable (you can set it beforehand to any value you like), he will try to run away, otherwise his state is set to ‘fire’, making him fire once and seting it back to ‘follow’.

We could include the “face back and fire” stuff into the flee behavior, but I thought you might want to order the enemy to fire once in a while, independent [COLOR=blue][COLOR=blue][COLOR=blue][COLOR=blue]of[/COLOR][/COLOR][/COLOR][/COLOR] it fleeing or not, so I kept both as independent states.

The point is: you want it to fire, use enemyState = ‘fire’; want it to run, enemyState = ‘flee’; want it to chase the player, enemyState = ‘follow’. You can list other behaviors like ‘patrol’, ‘hide’, etc, define what the enemy is suppose to do in that case and just set the state variable (enemyState) when the right condition is triggered.

Is it clear? Let me know if you need further help. :angel:

[size=2]i like[url=http://www.funingame.com/eve.html] your[url=http://www.funingame.com/guildwars.html] article[url=http://www.funingame.com/maplestory.html] very[url=http://www.allgamegold.com] much。[/size]

[quote=youngtrill;2327359]im not understanding how to do all what your saying lol i just needed a flash code

i tried this but it wont work

if(this.alive && this.hit==false && (_root.health<=50))
{
startPos = this._x
this.gotoAndStop(“walk”);
this._x -= 5;

if(this._x == startPos + 50) {
this._x +=5
}
}[/quote]
i think you can use distance formula and then randomise whatever you want within that range ,