Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 8 of 8

Thread: AI Enemies

  1. #1

    Afrostyle AI Enemies

    i am trying to make a game where the bad guys randomly walk about in a certain area. the enemies will attack you when you enter into there roaming around space. i have been working in flash for 4 years and cant think how to do it

    thanks
    Stupid Saint

  2. #2
    Code:
    if(Math.abs(_root.hero._x-_root.enemy._x)<100 && Math.abs(_root.hero._y-_root.enemy._y)<100){
              //enemy attacks
    }
    It's pretty much saying that if your hero's x and y positions are less than 100 pixels away from your enemies x and y positions, then your enemy should attack. You can change his attack radius by altering the distance of the x and y.

    Note: To make sure this works correctly, your registration point must be in the exact same place in all movieclips.

    Hope that helped!
    Last edited by Skribble; May 14th, 2008 at 09:33 PM.

  3. Quote Originally Posted by stupidsaint View Post
    i am trying to make a game where the bad guys randomly walk about in a certain area. the enemies will attack you when you enter into there roaming around space. i have been working in flash for 4 years and cant think how to do it

    thanks
    Stupid Saint
    How can you not figure this out if you have been doing flash for 4 years? I guess you spend most of that time on animation then XD

    Anyways, for the moving part a simple solution would be: would let every enemy pick a random location in the field, move there, and when it has reached that destination, repeat.

    Anyways, the moving part could be simple solution. Let every enemy pick a location, let every enemy move to that location, when an enemy reaches that location: repeat.

    Code:
    enemy.moveToLocation = new Point (getRandomNumber(0, Stage.Width), getRandomNumber (0, Stage.Height));
    
    enemy.OnEnterFrame = function () 
    {
    if (enemy.X > moveToLocation.X) { enemy.X -= enemy.movingSpeed; }
    else { enemy.X += enemy.movingSpeed; }
    // continue
    
    if (getDistance (enemy.X, enemy.Y, moveToLocation.X, moveToLocation.Y) < movingSpeed)
    {
    enemy.moveToLocation = new Point (getRandomNumber(0, Stage.Width), getRandomNumber (0, Stage.Height));
    }
    }
    getRandomNumber(min:Number, max:Number):Number
    {
    return (Math.floor(Math.random()*(max-min+1)) + min);
    }
    getDistance (x1:Number, x2:Number, x2:Number, y2:Number):Number
    {
    var xd = x1 - x2;
    var yd = y1 - y2;
    return ((xd * xd) + (yd * yd))^0.5;
    }
    The above code was quickly typed to give you an idea on how you could be doing this. Though, you may not want to use the geom classes for point, but rather create your own one. And also, there are better ways to do this

  4. #4
    Quote Originally Posted by ArmoredSandwich View Post
    Code:
    getDistance (x1:Number, x2:Number, x2:Number, y2:Number):Number
    {
    var xd = x1 - x2;
    var yd = y1 - y2;
    return ((xd * xd) + (yd * yd))^0.5;
    }
    I just notied ((xd * xd) + (yd * yd))^0.5; line. Is ^0.5 actually valid code for 'power'? For some reason I have been using Math.sqrt...

  5. #5
    Quote Originally Posted by ArmoredSandwich View Post
    How can you not figure this out if you have been doing flash for 4 years? I guess you spend most of that time on animation then XD

    Anyways, for the moving part a simple solution would be: would let every enemy pick a random location in the field, move there, and when it has reached that destination, repeat.

    Anyways, the moving part could be simple solution. Let every enemy pick a location, let every enemy move to that location, when an enemy reaches that location: repeat.

    Code:
    enemy.moveToLocation = new Point (getRandomNumber(0, Stage.Width), getRandomNumber (0, Stage.Height));
    
    enemy.OnEnterFrame = function () 
    {
    if (enemy.X > moveToLocation.X) { enemy.X -= enemy.movingSpeed; }
    else { enemy.X += enemy.movingSpeed; }
    // continue
    
    if (getDistance (enemy.X, enemy.Y, moveToLocation.X, moveToLocation.Y) < movingSpeed)
    {
    enemy.moveToLocation = new Point (getRandomNumber(0, Stage.Width), getRandomNumber (0, Stage.Height));
    }
    }
    getRandomNumber(min:Number, max:Number):Number
    {
    return (Math.floor(Math.random()*(max-min+1)) + min);
    }
    getDistance (x1:Number, x2:Number, x2:Number, y2:Number):Number
    {
    var xd = x1 - x2;
    var yd = y1 - y2;
    return ((xd * xd) + (yd * yd))^0.5;
    }
    The above code was quickly typed to give you an idea on how you could be doing this. Though, you may not want to use the geom classes for point, but rather create your own one. And also, there are better ways to do this
    i have spent the first year on learning the basics in flash drawing and little actionscript

    the second year i continued to make images and develop movies

    the third year i started to learn the slightly more advanced actionscript

    the fourth year i started to learn amazingly advanced actionscript, xml (reading and writing) and asp loading. (i am only 14 so i'm not very good at leaning anyway :-D)

    your code didn't really work but thanks for your help

    Stupid Saint

  6. Quote Originally Posted by misterooga View Post
    I just notied ((xd * xd) + (yd * yd))^0.5; line. Is ^0.5 actually valid code for 'power'? For some reason I have been using Math.sqrt...
    Yea it's the same thing, square root is the same thing as ^0.5.

    And my code would work if you just fixed the typos and completed it. As I said it was to give you a basic idea on how it would work. If you can't handle things like that, that's fine but don't expect complete codes laid out for you.

  7. #7
    Hey,
    Stupid saint, could you create a MC and animate it walking around? Then when your character hitTest's the MC, the MC shoots him? Just an idea

  8. #8

    Well...

    This is not a good AI, in my opinion, but, since this is what you want, here's a .fla file that does what I think you're looking for. Note that attributes like enemy speed and sight range can be set differently per enemy, if you prefer, but, since this is supposed to be a quickie and simple example, I used global values for all enemies (and also random positioning for them and the player). Also, the "sight" range is a square, you can do it a circle if you take the time and trigonometry to do so, but I'm not in the mood right now...

    The enemies "attack", in this example, consists only in following the player and getting a nastier look. They don't actually affect the playert ijn any way if they happen to catch him.

    Besides that, I'd strongly suggest you try to devise a better behavior. All this random patrolling doesn't sound good to me.
    Attached Files Attached Files
    Bruno Palermo
    When everything is catastrophic, thank the Gods things aren't worse!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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