0
votes

I was making a game in flash and I have a bit of an issue that I can't solve :S

Lets say the enemies are spawning from left to right, and lets say we have 3 types of enemies.

Right now I just write a for loop that generates amount of enemies of each type and places them in random position on the vertical scale (Math.random()*stage.stageWidth)

Now it happends sometimes that enemies intersecting with each other plus the waves are never the same if you restart the game.

I was thinking of the way to make a MovieClip like EnemyContainer and place therin manually enemies in formations or patterns and then controle them.

Problem is that my Enemy class constructor uses arguments like reference to the stage and speed etc and if I just drop my objects in the movieclip I get errors because I can't define the stage at that point.

Does anyone have an idea of how to make the waves of enemies with pattern?

Here is the constructor of the Enemy:

public function Enemy(stageRef:Stage, firerate:Number=1000, speed:Number=1) {

As you can see it takes 3 args, but when droping movieclip of the Enemy in the EnemyContainer movieclip I have an error because you cant define the stage.

Besides I am not sure if that idea with making one giant movieclip with patterns of enemies is the right way to do it.

Another thing is my differnet enemies types are in one MovieClip enemy on differend frames and I can't control it either if I drop them in one movieclip

1
It is not clear what your problem is? Do you have an error? If yes post some code. As far as the approach you are taking, I think it may work but the issue you encountered along the way is not really related to the initial problem of spawning enemies. - Antoine Lassauzay
If you want patterns, you can do that programmatically - you just need to figure out what works best for your game. As for the stage, make sure it's available before you pass it. In the 1st couple lines of code there should be: this.addEventListener(Event.ADDED_TO_STAGE, this.ready); - Gone3d

1 Answers

0
votes

never mind I think I found the right way to do it.

The problem was that I was adding the object of enemy to the EnemyContainer.

Now I just add some dummy MovieClip and use a for loop to iterate through all the children of the EnemyContainer and create the Enemy with the position of the child.

Like this:

public function createEnemyFromContainer():void {
        enemyContainer = new EnemyContainer();
        //stage.addChild(enemyContainer);
        for (var i=0; i<enemyContainer.numChildren; i++) {
            var _tempEnemy_mc:Enemy = new Enemy(stage,1000,0.2+Math.random()*0.3);;

            stage.addChild(_tempEnemy_mc);

            _tempEnemy_mc.x = enemyContainer.getChildAt(i).x;
            _tempEnemy_mc.y = enemyContainer.getChildAt(i).y;
            _tempEnemy_mc.gotoAndStop("tank");
            _tempEnemy_mc.enemyCanon.gotoAndStop("tank");

            enemyObjectsArray.push(_tempEnemy_mc);
        }
    }