0

I was reading a tutorial about creating multiple levels, and the below really interested me on how i should go about this.

It may seem natural to make one class per level, with each class extending AvoiderGame, and use events to switch between them. So, we might have classes named AvoiderGameLevelOne, AvoiderGameLevelTwo, etc., and let each one fire off a “NavigationEvent.NEXT_LEVEL” when appropriate. Presumably then the document class would listen for this event, and when it heard it, it would run “playScreen = new AvoiderGameLevelTwo()” (or whichever level was appropriate), and pass through all the information such as score and time to this new playScreen instance.

I'm not entirely sure on how to go about this. I put my stage, which is an array of tiles in a class called level1, level2, etc and had it extend my main class. Just to check if everything works, I added a public static var called levelArray in my main, which is a blank array. Then in level1, I pushed my array into levelArray.

So for my level1 class

package  {

    public class Level1 extends Main {

        public var floor1:Array = new Array();
        floor1[0] = [2,1,1,1,1,1,2];
        floor1[1] = [1,1,1,1,1,1,1];
        floor1[2] = [1,1,1,2,1,1,1];
        floor1[3] = [1,1,1,1,1,1,1];
        floor1[4] = [1,1,1,2,1,1,1];
        floor1[5] = [1,1,1,1,1,1,1];
        floor1[6] = [2,1,1,1,1,1,2];

        public function Level1() {

            Main.levelArray.push(floor1);
        }


    }

}

Doesn't seem to be working. levelArray comes up as blank. Might be because the two classes aren't communicating with each other correctly? Any ideas if I am approaching this the correct way?

  • Not really relevant to your problem, but I don't think you want your Level class to extend Main do you? I mean 'Level1' isn't a type of 'Main' is it? – Cadin Sep 05 '13 at 23:39
  • True. I just thought that having this refer to the Main might have them communicate. Should I just have it extend MovieClip? – Bindlestick Sep 06 '13 at 13:21
  • Depending on how complex the level classes will be, you might want to make a base Level class and have all your levels extend that. Level can have generic methods for communicating with a Level object. Most likely you'd instantiate a new Level in Main and communicate with it that way. The Level class doesn't necessarily need to extend anything (unless it IS actually a MovieClip). – Cadin Sep 06 '13 at 17:47

1 Answers1

2

I dont know if the rest of your concept is sound, but I think the syntax is off for the part you have shown. try:

package  {

    public class Level1 extends Main {

        public var floor1:Array = new Array( [2,1,1,1,1,1,2],
                                             [1,1,1,1,1,1,1],
                                             [1,1,1,2,1,1,1],
                                             [1,1,1,1,1,1,1],
                                             [1,1,1,2,1,1,1],
                                             [1,1,1,1,1,1,1],
                                             [2,1,1,1,1,1,2]
                                           );

        public function Level1() {
            Main.levelArray = floor1;
        }
    }

}

EDIT: if the only thing distinct about each level is the array that forms the floor, you may consider the fact that you do not need a new class for each level, just a new array. you can define the arrays for each level in the class that is super to this one and then just replace them with each progression.

NappingRabbit
  • 1,888
  • 1
  • 13
  • 18
  • This is the correct answer. You can't run code outside of functions in class-based AS3. If you want to add something to the array, you either need to do it in the constructor, another function, or do it within the array declaration, as is done in the above answer. – Josh Sep 05 '13 at 19:22
  • I plan on adding different enemies for different levels. Thanks, it looks like it works, I just need to figure out the extends portion of the level class. – Bindlestick Sep 06 '13 at 14:03