PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
How to configure random enemy AI movement?
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: How to configure random enemy AI movement?

Hybrid View

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    28

    How to configure random enemy AI movement?

    Hi guys,

    I'm currently making a 2D shoot em up game where each enemy created will follow the player around the level. I'm wondering for any suggestions or code tips on how to make the enemy AI/movement randomise and also to make the enemies stay away from each other as there is only collision setup between player vs enemy, not enemy vs enemy. Here is my code I've done so far for enemy AI:
    Actionscript Code:
    public class MainShapeShooter14 extends MovieClip
       {
    public var level:int = 1;
    public var enemies:Array;    
     private var _randomX:uint;
        private var _randomY:uint;
    private var maxEnemyStep:int = 2
    var _timer:Timer;

    public function MainShapeShooter14()
    _timer = new Timer(5000);
    enemies = new Array();
        makeEnemies();

    public function makeEnemies():void{
       _timer.start();
       for(var i:int = 0; i < level; i++){
         var tempEnemy:Enemy = new Enemy();
         addChild(tempEnemy);
         var randomXPosition = Math.round(Math.random() * stage.stageWidth);
        var randomYPosition = Math.round(Math.random() * stage.stageHeight);
        tempEnemy.x = randomXPosition;
        tempEnemy.y = randomYPosition;
       
    enemies.push(tempEnemy);
        tempEnemy.gotoAndStop(1);
         addEventListener(Event.ENTER_FRAME, onEnterFrame);
         tempEnemy.addEventListener(Event.ENTER_FRAME, onEnemyMove);
        _timer.addEventListener(TimerEvent.TIMER, onUpdateTime);
         }
        }
     function onEnemyMove(event:Event):void{
        var enemy:Enemy = Enemy(event.currentTarget);
         var xDiff:Number = player.x - enemy.x;
         var yDiff:Number = player.y - enemy.y;
       
        //if they are further than maxEnemyStep away in x
         if (Math.abs(xDiff) > maxEnemyStep){
            if (xDiff < 0){
              xDiff = -maxEnemyStep;
            }else{
              xDiff = maxEnemyStep;
            }
         }
         //if they are further than maxEnemyStep away in y
         if (Math.abs(yDiff) > maxEnemyStep){
            if (yDiff < 0){
              yDiff = -maxEnemyStep;
            }else{
              yDiff = maxEnemyStep;
            }
         }
         enemy.x += xDiff;
         enemy.y += yDiff;
         xDiff *= _friction;
         yDiff *= _friction;
         enemy.gotoAndStop(1);
        }

    Thank you for your time and reading all that and any help/suggestions would be greatly appreciated.

    Jonesy

  2. #2
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    Check this stuff out:
    http://www.red3d.com/cwr/steer/
    Seems like what you need.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

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