A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Having problems with setting up enemy spawn state machine

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

    Having problems with setting up enemy spawn state machine

    Hi guys,

    I'm currently developing my own 2D shoot em-up and using a book called "The essential guide to Flash games" as reference to create my own enemy spawn state machine where the game keep spawning enemies (game goes on forever till player dies). I attempted to trigger each enemy spawn occuring to the game's level with an variable called "chance".

    With my current attempts, the game either only spawns one enemy and spawns no others or doesn't spawn any enemies at all. I was wondering anyone here has any suggestions/tips for this issue I'm having?

    Here is the example of the code from the book I've used:

    Code:
    public function Main()
        {
              level = 1;
          score = 0;
          chances = 0;
    
     enemies = new Array();
        makeEnemies();
        }
    public function makeEnemies():void {
                var chance:Number = Math.floor(Math.random() *100);
                var tempEnemy:MovieClip;
                if (chance < 2 + level)
                    tempEnemy = new EnemyImage()
                                    tempEnemy.y = 435;
                    tempEnemy.x = Math.floor(Math.random()*515)
                    addChild(tempEnemy);
                    enemies.push(tempEnemy);
                     }
    function checkCollisionWithEnemies(bullet:MovieClip)
                                    {
                                      if (enemy.meter.width < 1)
                        {
                            enemy.stop();
                            swapProperies(enemy,b);
                            removeChild(enemy);
                            enemy = null;
                            level ++;
                      }
    Occuring to the book the lines :

    Code:
    "var chance:Number = Math.floor(Math.random() *100);
    "if (chance < 2 + level)"
    should create +2 enemies according to the game's level, but no enemies spawn at all. I also tried a different if method with "if(level >= 1)" but that only spawns one enemy.

    Does anyone have any suggestions of what I'm doing wrong? Any help would be greatly appreciated.

    Thanks

    Jonesy

  2. #2
    Senior Member
    Join Date
    May 2009
    Posts
    138
    Try this. Also, it's good practice to indent your code a little more consistently. It makes it easier to read and spot mistakes like your line "if (chance < 2 + level)" not having curly braces. I also recommend the HIGHLIGHT=Actionscript tag when posting AS3 code.

    Actionscript Code:
    public function Main()
    {
        level = 1;
        score = 0;
        enemies = new Array();
        makeEnemies();
     }

    public function makeEnemies():void
    {
        for(var i:int = 0; i < 2 + level; i++)
        {
            var tempEnemy:MovieClip;
            tempEnemy = new EnemyImage()

            tempEnemy.y = 435;
            tempEnemy.x = Math.floor(Math.random()*515)
            addChild(tempEnemy);
            enemies.push(tempEnemy);
        }
    }

    function checkCollisionWithEnemies(bullet:MovieClip)
    {
        if (enemy.meter.width < 1)
        {
            enemy.stop();
            swapProperies(enemy,b);
            removeChild(enemy);
            enemy = null;
            level ++;
         }
    }

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    28
    Thanks redjag for the tip - using AS Highlight is a lot better.

    Sadly I remain to have the same problem where the loop isn't spawning new enemies when old enemies are killed with my new code. I'm using a "level" instead of "chance" to determine the spawn waves, for every enemy level goes up by 1 triggering a new enemy to spawn. I'm not sure where I am going wrong but here goes ....

    Actionscript Code:
    public class MainShapeShooter11 extends MovieClip
       {
            public var score:int = 0; //Game score
            public var level:int = 1;
            public var enemies:Array;

    public function MainShapeShooter11()
        {
     enemies = new Array();
        makeEnemies();
            }
       
      public function makeEnemies():void{
       for(var i:int = 0; i < 2 + level; i++){
         var tempEnemy:Enemy = new Enemy();
         addChild(tempEnemy);
         enemies.push(tempEnemy);
         tempEnemy.gotoAndStop(1);
         addEventListener(Event.ENTER_FRAME, onEnterFrame);
         tempEnemy.addEventListener(Event.ENTER_FRAME, onEnemyMove)}
    }

      function onEnterFrame(event:Event):void{
      var enemy:Enemy = enemies[i];
      for (var i:int = 0; i < enemies.length; i++){  
        if (player.hitTestObject(enemy)){
            if (player.currentFrame == 1 || player.currentFrame == 3){
        health.meter.width--;
        player.nextFrame();
    }
    Collision.block(player, enemy);        
    }
    else if (player.currentFrame == 2|| player.currentFrame == 4){
        player.prevFrame();
    }
    Collision.block(player, enemy);
    break;
       }
    }
     function checkCollisionWithEnemies(bullet:MovieClip){
    if (enemy.meter.width < 1){
                           
        removeChild(enemy);
        enemy = null;
        level ++;
        enemies.splice(i, 1);
             i--;
        trace ("Level" + level);
        levelText.text = level.toString();         
    }

    Also the other thing is for some reason, three enemies are created at the beginning of the game instead of one. Like I said before if anyone has any suggestions or tips of where I'm going wrong would be greatly appreciated.

    Thanks

    Jonesy

  4. #4
    Senior Member
    Join Date
    May 2009
    Posts
    138
    Actionscript Code:
    for(var i:int = 0; i < 2 + level; i++){

    This is the line that controls the number of spawns. If you want it to spawn 1 enemy on level 1, 2 on level 2, etc etc then change it to i < level instead of i < 2 + level.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    28
    Thanks redjag again, duh I don't know why I didn't think of that? But good one, thanks

    Here's another issue/question although I think this probably deserves its own topic/thread actually but here goes anyway...

    Before I setup this spawn enemy state machine I was using tween objects for a single enemy for it's AI/movement which was placed on stage already.
    What I was wondering if its possible to use tween objects for all new enemies added to the stage for their movement/ai from the enemy spawn loop? Here is my code:

    Actionscript Code:
    public function makeEnemies():void{
       for(var i:int = 0; i < level; i++){
         var tempEnemy:Enemy = new Enemy();
         addChild(tempEnemy);
         enemies.push(tempEnemy);
         tempEnemy.gotoAndStop(1);
         tempEnemy.addEventListener(Event.ENTER_FRAME, onEnemyMove);
         
    _tweenX = new Tween(tempEnemy, "x", Regular.easeInOut, tempEnemy.x, _randomX, 45, false);
    _tweenY = new Tween(tempEnemy, "y", Regular.easeInOut, tempEnemy.y, _randomY, 45, false)
    _tweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinishX);
         _tweenY.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinishY);
    }
     function onMotionFinishX(event:Event){
        var tempEnemy:Enemy = new Enemy();
        var tweenObject:Tween = tempEnemy(event.currentTarget);  
          //Calculate new start and finish positions
          tweenObject.begin = tempEnemy.x;
          tweenObject.finish = Math.ceil(Math.random() * 400);
          tweenObject.finish = player.x;
          //Start the Tween object playing again
          tweenObject.start();
    }
     function onMotionFinishY(event:Event){
       var tweenObject:Tween = tempEnemy(event.currentTarget as Tween);
          tweenObject.begin = tempEnemy.y;
          tweenObject.finish = Math.ceil(Math.random() * 400);
          tweenObject.finish = player.y;
          tweenObject.start();}
      }
     function onEnemyMove(event:Event):void{
         var enemy:Enemy = Enemy(event.currentTarget);
         enemy.vx = enemy.x - enemy.oldX;
         enemy.vy = enemy.y - enemy.oldY;
         enemy.oldX = enemy.x;
         enemy.oldY = enemy.y;
         enemy.gotoAndStop(1);

        if (enemy.x > stage.stageWidth){ /// stage perimeters
             enemy.vx *= _bounce;
             enemy.x = stage.stageWidth;}
        if (enemy.x < 0){  /// stage perimeters
             enemy.vx *= _bounce;
             enemy.x = 0;}

    My logic was that as soon as the new enemy object was created/added to stage it would become a tweenObject but either receive a error message stating that tempEnemy is undefined method or 1067 error: Implicat coercion of a value of type Enemy to an unrelated type fl.transitions:Tween.

    This is line of code - var tweenObject:Tween = tempEnemy(event.currentTarget); that makes sense? Or am going in the complete wrong direction for such functionality? Yet again any help/suggestions would be greatly appreciated.

    Thanks

    Jonesy

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