Results 1 to 5 of 5

Thread: make enemy run away and shoot at me

  1. #1

    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

  2. #2
    207
    posts
    Registered User
    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.

  3. #3
    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
    }
    }

  4. #4
    Ideally you should use states. Your enemy would have a list of states, which one with a certain behavior, triggers would set the enemy's current state. For instance:

    Code:
    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 of the variable between brackets and it has a list of possible values (cases) and what to do for each of those values. everything between 'case <casename>:' and 'break;' is executed in case the value of 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 of 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.
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

  5. #5
    Quote Originally Posted by youngtrill View Post
    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
    }
    }
    i think you can use distance formula and then randomise whatever you want within that range ,

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012