Vlad
Vlad

Reputation: 2773

Game stucture design

Ok. I have a question about AS3 game structure. How to structure simple shooter game.

I have a main Hero and ships that shoot bulets. I want to add two levels but I am not sure how to structure them properly or which way is properly.

I have main class that holds everything and should switch trough levels. So I separate each level as a separate class.

Sample code

package
{
    // imports

    public class Main extends Sprite
    {
         // properties
         private var testLevel:Level1;

         public function Main():void 
         {
    if (stage) init();
    else addEventListener(Event.ADDED_TO_STAGE, init);
     }

         private function init(e:Event = null):void 
     { 
             testLevel  = new Level1();
             addChild(testLevel);
         } 
    }
}

// Level1 code
package Levels
{
    // imports
    public class Level1 extends Sprite
{
         // properties
         private var ship:Ship;

         public function Level1(stage:Object) 
     {
            // do some stuff
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
         }

         public function onEnterFrame(e:Event):void
     {
              // do some stuff
         }

    }
}

So According to this code my question is: Should I add ship bullets using separate ENTER_FRAME function inside Ship or should I add them in the level loop?

I can do both ways but which is better for performance and for mintenance, because I plan to add some actions when the bulet reaches the end of the sceen or when hits the Hero.

Upvotes: 0

Views: 138

Answers (2)

Azzy Elvul
Azzy Elvul

Reputation: 1438

From my opinion - make a level engine and describe a level with xml instead of createing a class for each level. Make a shoot engine that holds a bulletes and update tham. Make a collision enginge to check the colisions. A good example is code of Waste Invaders. link Check src / com / shooty / engine code this will help you a lot.

Upvotes: 1

danii
danii

Reputation: 5703

This is a though compromise:

  • For performance is better to have one single ENTER_FRAME listener.

  • But for maintenance it should be better to have separate update functions inside the Ship class and inside each object (for example, Enemy class, Bullet class etc) in the game.

So usually the preferred method to deal with this and get the best of both options is to have one main ENTER_FRAME listener on your main class, which is usually referred to as the Main Game Loop. This should be the only ENTER_FRAME listener running in the game. From this Game Loop you should invoke an update function for each object currently in the game, which is the responsible for updating the object's position, status, etc inside the game.

Upvotes: 3

Related Questions