Author Topic: How to approach Levels creation.  (Read 1300 times)

Aenopix

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
How to approach Levels creation.
« on: Wed, Jul 13, 2011 »
Hi guys, i dont know how to approach the level creation for a game im trying to make. Im using Dame map editor and loading a level doing this in Playstate.
 
Code: [Select]
public class PlayState extends FlxState
{
                private var level_01:BaseLevel;
override public function create():void
{
level_01 = new Level_1();
}

That creates the level and show it, but what if i want to have more levels and when u reach a door you go to the next level?, How i would do that?, im thinking of putting the level on a variable in an array, then loading each level going throught the array to know what level you are or something like that.

Basically i dont know how i can load more levels so when u get to a point u go to the next level, removing the last level, transitioning to the next level.

Thanks.

felipe

  • Member
  • **
  • Posts: 36
  • Karma: +0/-0
  • games!
    • View Profile
    • i love pixel
Re: How to approach Levels creation.
« Reply #1 on: Wed, Jul 13, 2011 »
This is what I've done for creating levels at playstate

Code: [Select]
Package
{

     import org.flixel.*;
     
     public Class PlayState extends FlxState
     {
          private var _level:Number;
          public function PlayState(level:Number):void
          {

                    level = _level;
          }
         
          override public function create():void
          {

                   if(_level == 1)
                   {
                         //Load level 1 data
                   }
                   
                    super.create();

          }

          override public function update():void
          {

                   if(_level == 1)
                   {
                        //level 1 loops ( collisions, score++,etc..)
                   }
           
                   changeLevel();

                   super.update();
          }

          private function changeLevel():void
          {
                  if( condition to change level )
                  {
                        FlxG.switchState(new PlayState(_level+1);
                  }
                  else
                  {
                      //replay game or what ever.
                  }
          }


     }

}

screenberg

  • Guest
Re: How to approach Levels creation.
« Reply #2 on: Wed, Jul 13, 2011 »
You defenitly need an Array, Object or Class to store the level data.
I'm doing this often by using a static World Class.
Code: [Select]
package
{
  import com.adobe.serialization.json.JSON;
 
  import de.screenberg.flixel.FLXUtils;
 
  import flash.utils.ByteArray;
 
  import org.flixel.FlxG;
  import org.flixel.FlxPoint;
  import org.flixel.FlxState;
 
  public class World
  {
   
    [Embed(source='../data/rooms/room-test.tmx', mimeType="application/octet-stream")]
    public static var Map_0:Class;
   
    [Embed(source='../data/rooms/room-1.tmx', mimeType="application/octet-stream")]
    public static var Map_1:Class;
   
    private static var _currentMap:XML;
   
    public static const GRAVITY:FlxPoint = new FlxPoint(200, 200);
   
    public static var tileHeight:int;
    public static var tileWidth:int;
   
   
    public static const ROOM_MAPS:Object = {
      "room_0":new Map_0(),
      "room_1":new Map_1()
    }
   
    public static function get currentMap():XML
    {
      /*var file:ByteArray = ROOM_MAPS["room_"+FlxG.level];
      var str:String = file.readUTFBytes(file.length);*/
      _currentMap = new XML(ROOM_MAPS["room_"+FlxG.level]);
      return _currentMap;
    }
   
    public static function set currentMap(value:XML):void
    {
      _currentMap = value;
    }
   
    public static function getLayerByName(Name:String):XMLList{
      //trace("getLayer", Name, _currentMap.layer.(@name == Name));
      return _currentMap.layer.(@name == Name);
    }
   
    public static function getObjectgroupByName(Name:String):XMLList{
     // trace("getObjectgroupByName", Name, _currentMap.objectgroup.(attribute("name") == Name));
      return currentMap.objectgroup.(@name == Name);
    }
   
    public static function getObject(xml:*):Object{
      // trace("getObjectgroupByName", Name, _currentMap.objectgroup.(attribute("name") == Name));
      var obj:Object = new Object;
      obj["attributes"] =  {};
      for each (var attr : XML in xml.attributes())
      {
        obj["attributes"][String(attr.name())] = attr;
      }
     
      obj["properties"] = {};
     
      for each(var item : XML in xml.properties.property){
        obj["properties"][item.@name] = item.@value;
      }
      return obj;
    }
  }
}

Then I'm switching between the states like filipe does. But I don't pass through the current level count. Instead I'm increasing FlxG.level.
Code: [Select]
      _currentMap = new XML(ROOM_MAPS["room_"+FlxG.level]);
Code: [Select]
override public function create():void
{
    currentLevel = World.currentLevel;
}

zadvornykh

  • Active Member
  • ***
  • Posts: 205
  • Karma: +1/-0
    • View Profile
Re: How to approach Levels creation.
« Reply #3 on: Wed, Jul 13, 2011 »
I like using the template method.

You create a class, say PlayState.as which holds all the logic common to all levels. Then I create protected functions in places where I need to override level specific settings. For each level, I create a class which extends PlayState, and override those methods.

Something like this:

PlayState.as
Code: [Select]
protected var levelData:String;

override public function create():void
{
 createHero();
 createLevel();
 createEnemies();
}

protected function createHero():void
{
}

protected function createLevel():void
{
}

protected function createEnemies():void
{
}

I also have a separate class handling the level changing logic.