A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: A.I. in action script?

  1. #1
    Join Date
    Mar 2003

    A.I. in action script?

    Hello there,

    Im creating a fighting game (like street fighter) the problem Im having now is that I need to figure out how to create an AI for the enemy - right now all i have is this :

    newx = _root.philo._x;
    distx = int(getProperty(this, _x)-newx);
    z = (distx*distx)+(disty*disty);
    hyp = Math.sqrt(z);
    speed = 50;
    distance = int(hyp/speed);
    movex = distx/distance;
    movey = disty/distance;
    if (distance<=0) {
    } else {
    setProperty(this, _x, getProperty(this, _x)-movex);
    setProperty(this, _y, getProperty(this, _y)-movey);
    distance = distance-1;

    What this does is make the enemy walk towards "philo" -

    then during the frames I have him punching. So basically what I need to know is any ideas you might have to make him harder to kill.

    Right now if you go to him and keep pressing one of the hit keys - he dies right away with out fighting back - this is a real ABSTRACT question - I just want to see what people have to say.


  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Los Angeles
    First off, you'll probably get a lot more opinions on this if you post in the games forum.

    One thing that might be interesting is to have your computer-controlled guys respond to the current state (or move-number) of the opponent.

    In a lot of combat games, there are a number of different moves you can trigger by holding down various keys, or hitting certain keys in sequence. For example, if you hit SHIFT->RIGHT->RIGHT->UP your guy might do a flying kick with a half-twist, or something. Presumeably, each of these special moves corresponds to a number. So you might have a table that looks like this:


    etc. etc. - a unique number for each move, a unique animation for each of those moves, and a unique way of triggering the move.

    Now this is all for the guy the player is controlling. So the idea is to make the computer do different things based on what the player is doing.

    So basically, you want to give the computer a counter-state which reacts to the player's state, depending on the proximity to the player.

    OPP_ADVANCE = 0;
    OPP_RETREAT = 1;
    OPP_KICK = 2;

    So you might end up with something like:


    if (player.mode == PLAYER_ADVANCE) {
    if (closeToPlayer())
    myMode = OPP_RETREAT;
    myMode = OPP_ADVANCE;
    // maybe randomly switch modes here...
    else if (player.mode == PLAYER_RETREAT) {
    myMode = OPP_ADVANCE;
    else if (player.mode == PLAYER_KICK) {
    if (inPositionForKick()) {
    myMode = OPP_GETKICKED;
    else {
    myMode = OPP_AVOIDKICK;
    // etc etc...

    Something else that is worth discussing here is timing. You can't be constantly changing your mode (e.g. you don't want to do this from an onEnterFrame handler) because many of these modes should trigger animations that have a fixed playback. For example, if you are in the middle of a kick, you can't suddenly, in mid-air switch to retreating. You would want to make these decisions of which mode to switch to when the animation from the previous mode is finished. At the end of each animated sequence, you might want to call a routine "selectState" that triggers the next mode, and looks something like the above code.

    Also, timing is probably critical for determining whether the player landed one on you or not. If you are are close to the end of an animation, than you might have sufficient time to react to the player, but if you are in the middle of something, and the player has a good counter-move, than the player should probably manage to score on you.

    Some of your modes (and their animations) will need multiple outcomes. A kick might end up you landing on your feet OR a kick might end up with you getting flipped by your opponent. One way to accomplish this is to divide full-moves up into sub-moves.



    Needless to say, making a good opponent AI is going to be a LOT of work.

    Last edited by jbum; 10-06-2004 at 08:01 PM.

  3. #3
    Flash Guru
    Join Date
    Apr 2007
    Wellington << New Zealand

    Brilliant Advice

    Good lord, well done on your brilliant advice. This is amazing how you gave the intructions so cleary, I couldn't have said it better myself.

    And yes, I do take notice of the dates on posts

Posting Permissions

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

Click Here to Expand Forum to Full Width

HTML5 Development Center