Question

I have an Enemy Class(which holds all the movie clips that are enemies), a Level class(which lists the layout of the levels in an array) and my Main document class(which produces the layout of the levels in tiles). My goal is to have different enemies for each level.

Enemy Class:

private var enemy1:Enemy1 = new Enemy1();
private var enemy2:Enemy2 = new Enemy2();

public var enemyArray:Array = new Array(enemy1, enemy2);

Level Class:

var enemies:Enemy = new Enemy();
levelStageNumber = 1;
if (levelStageNumber == 1) { floor1F(); Main.floor = floor1; }
private function floor1F():void
        {
            floor1 = new Array();
            floor1[0] = [1,1,1,1,1,1,1];
            floor1[1] = [1,1,1,1,1,1,1];
            floor1[2] = [1,1,1,2,1,1,1];
            floor1[3] = [2,1,1,2,1,1,2];
            floor1[4] = [1,1,1,2,1,1,1];
            floor1[5] = [1,1,1,1,1,1,1];
            floor1[6] = [1,1,1,1,1,1,1];

            addChild(enemy2.enemyArray[0]);

        }

Main Class:

public static var floor:Array = []; //for the main class to talk to the level class
private var level:Level = new Level();

addChild(level);

    for (var Y:int=0; Y<floor.length; Y++)
            {
                for (var X:int=0; X<floor[Y].length; X++)
                {
                    var cell:MovieClip = new Tile(X,Y);
                    cell.gotoAndStop(floor[Y][X]);
                    cell.x = ((X-Y)*level.tileh)+365;
                    cell.y = ((X+Y)*level.tileh/2)+70;
                    addChild(cell);
                }
            }

Maybe I'm going about this whole setup the wrong way, but I feel there is an easier way to accomplish what I'm doing. So I have 2 problems. First, how can I add the two enemies from the enemyArray? Secondly, the enemy right now appears underneath the tile, probably because the Main class is the one generating the tiles. Is there any way I can somehow get the enemies to be added to the Main class?

Was it helpful?

Solution

(1) You can add any enemy in the array to the display list by specifying it's index in the array :

addChild(enemyArray[0]);

addChild(enemyArray[1]);

(2) The order in which you add something to the stage's display list, determines what is on top.

My advice would be that you add the floor tiles to the Level class, then add the enemies.

Also for the level, it's probably better if you have the enemies array there as opposed to in the Enemy class. enemyArray contains a collection of enemies, so you could have that in your level, as it represents the enemies on that level.

The Enemy class should only have properties and methods specific to an Enemy. Like move or shoot would be methods and hitPoints or power might be properties of an Enemy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top