Author Topic: help with this dungeon generator problem  (Read 1736 times)

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
help with this dungeon generator problem
« on: Fri, Dec 30, 2011 »
http://roguebasin.roguelikedevelopment.org/index.php/Complete_Roguelike_Tutorial,_using_python%2Blibtcod,_part_3#Dungeon_building_blocks  hi! I'm trying to convert the python code above to flixel and I'm getting errors. Anyway, here is my code :

package com 
{
   import org.flixel.*;
   /**
    * ...
    * @author
    */
   public class Level extends FlxGroup
   {
      private const _tileWall:uint = 1;
      private const _tileEmpty:uint = 0;
      private var _tileMap:FlxTilemap;
      private var _tilesX:uint; //the number of tiles per row
      private var _tilesY:uint; //the number of tiles per column
      private var _maxRoomSize:uint; //the max width of each rooms
      private var _minRoomSize:uint; //the max height of each room
      
      private var _levelArray:Array;
      private var _finalArray:Array;
      
      private var _maxNumRooms:uint;
      private var _numRooms:uint;
      private var _rooms:Array;
      private var _tileSize:uint;
      
      //starting position of player
      public var playerX:uint;
      public var playerY:uint;
      
      
      [Embed(source = "../res/tileMap.png")] private const tileMapPNG:Class;
      
      public function Level(tilesX:uint,tilesY:uint,maxNumRooms:uint,tileSize:uint):void
      {
         _tilesX =tilesX;
         _tilesY = tilesY;
         _tileSize = tileSize;
         _maxNumRooms = maxNumRooms;
         _numRooms = 0;
         _minRoomSize = 6;
         _maxRoomSize = 10;
         
         
         generate_level();
         
         
         
         _tileMap = new FlxTilemap();
         _tileMap.loadMap(FlxTilemap.arrayToCSV(_finalArray, _tilesX), tileMapPNG, _tileSize, _tileSize);
         add(_tileMap);
         FlxG.watch(this,'_levelArray');
      }
      
      
      public function generate_level():void
      {
         
         fillLevel();
         
         //trace(_levelArray);
         
         generateRooms();
         generateFinalMap();
         
                     
         
         
         
         
         
         
         
      }
      
      
      
      
      public function fillLevel():void
      {
         _levelArray=new Array();
         for (var xi:uint = 0; xi < _tilesX; xi++)
         {
            _levelArray.push(new Array);
            for (var yi:uint = 0; yi <_tilesY; yi++)
            {
               _levelArray[xi][yi]=_tileWall;
            }
         }
      }
      
      
      
      
      
      public function generateRooms():void
      {
         _rooms = new Array();
         //var room:Room = new Room(new FlxRect(50, 10, 10, 5));
         //
         //createRoom(room);
         for (var i:uint = 0; i < _maxNumRooms; i++)
         {
            var w:uint =  _minRoomSize + Math.round(Math.random() * (_maxRoomSize-_minRoomSize));
            var h:uint = _minRoomSize + Math.round(Math.random() * (_maxRoomSize-_minRoomSize));
            var x:uint = Math.floor(Math.random() * _tilesX-_maxRoomSize-1);
            var y:uint = Math.floor(Math.random() * _tilesY-_maxRoomSize-1);
            trace(_levelArray.length);
         
         
            var room:Room = new Room(new FlxRect(x, y, w, h));
            createRoom(room);
            
         }
         
            
            
         
         

         
      }
      
      
      public function createRoom(room:Room):void
      {
         
         trace(_levelArray.length);
         
         for (var xi:uint = room.x+1; xi < room.x2; xi++)
         {
            for (var yi:uint = room.y+1; yi < room.y2; yi++)
            {
               _levelArray[xi][yi] = _tileEmpty;
            }
         }
         
         
      }
      
   
      
      
      
      
      
      public function generateFinalMap():void
      {
         _finalArray = new Array();
         for (var yi:uint = 0; yi < _tilesY; yi++)
         {
            for (var xi:uint = 0; xi < _tilesX; xi++)
            {
               _finalArray.push(_levelArray[yi][xi]);
            }
         }
      
      }
         
   }

}


sorry for the bad code. The error I'm getting is this:
TypeError: Error #1010: A term is undefined and has no properties.
   at com::Level/createRoom()[D:\Game Development\Flash Projects\RogueLike\src\com\Level.as:133]
   at com::Level/generateRooms()[D:\Game Development\Flash Projects\RogueLike\src\com\Level.as:111]
   at com::Level/generate_level()[D:\Game Development\Flash Projects\RogueLike\src\com\Level.as:62]
   at com::Level()[D:\Game Development\Flash Projects\RogueLike\src\com\Level.as:44]
   at com::PlayState/create()[D:\Game Development\Flash Projects\RogueLike\src\com\PlayState.as:14]
   at org.flixel::FlxGame/switchState()[D:\Game Development\Flash Projects\RogueLike\src\org\flixel\FlxGame.as:499]
   at org.flixel::FlxGame/step()[D:\Game Development\Flash Projects\RogueLike\src\org\flixel\FlxGame.as:544]
   at org.flixel::FlxGame/onEnterFrame()[D:\Game Development\Flash Projects\RogueLike\src\org\flixel\FlxGame.as:453]


Sometimes the code works and sometimes it gets the error. I really can't find the problem and I've been trying to solve this so I would appreciate any help. Thanks! :)
Check out my game journal: http://alternatdev.wordpress.com

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: help with this dungeon generator problem
« Reply #1 on: Fri, Dec 30, 2011 »
The problems it's telling you to look out for are on the lines it specifies. Which are these:

Code: [Select]
ypeError: Error #1010: A term is undefined and has no properties.
   at com::Level/createRoom()[D:\Game Development\Flash Projects\RogueLike\src\com\Level.as:133]
   at com::Level/generateRooms()[D:\Game Development\Flash Projects\RogueLike\src\com\Level.as:111]
   at com::Level/generate_level()[D:\Game Development\Flash Projects\RogueLike\src\com\Level.as:62]
   at com::Level()[D:\Game Development\Flash Projects\RogueLike\src\com\Level.as:44]
   at com::PlayState/create()[D:\Game Development\Flash Projects\RogueLike\src\com\PlayState.as:14]
   at org.flixel::FlxGame/switchState()[D:\Game Development\Flash Projects\RogueLike\src\org\flixel\FlxGame.as:499]
   at org.flixel::FlxGame/step()[D:\Game Development\Flash Projects\RogueLike\src\org\flixel\FlxGame.as:544]
   at org.flixel::FlxGame/onEnterFrame()[D:\Game Development\Flash Projects\RogueLike\src\org\flixel\FlxGame.as:453]

Lines 44,62, 111,and 133 all seem to have a problem when running into line 14 on your playstate. 'A term undefined or has no properties', uh, means that the values that you have on those lines, I think, aren't being set anywhere at the time they are called by the playstate on line 14.

Maybe posting your playstate as well would help narrow it down?
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: help with this dungeon generator problem
« Reply #2 on: Fri, Dec 30, 2011 »

Thanks for the reply! I posted my 'PlayState" here. Thanks!



package com 
{
   import org.flixel.*;
   /**
    * ...
    * @author
    */
   public class PlayState extends FlxState
   {
      public var testLevel:Level;
      
      override public function create():void
      {
         testLevel = new Level(100, 100, 20, 8);
         add(testLevel);
         super.create();
      }
      
      public function PlayState()
      {
         
      }
      
   }

}
Check out my game journal: http://alternatdev.wordpress.com

JAH2488

  • Member
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: help with this dungeon generator problem
« Reply #3 on: Fri, Dec 30, 2011 »
it doesn't look like your level class is being imported. Is it in the same directory?

Also, I'm not sure if super.create is needed in the playstate, but I could  be wrong.

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: help with this dungeon generator problem
« Reply #4 on: Fri, Dec 30, 2011 »
Quote
Also, I'm not sure if super.create is needed in the playstate, but I could  be wrong.

No, it is not needed. Your also missing the update function. I'm pretty sure that's needed even if nothing happens yet but I could be wrong there. Plugging your code into my tests, the only error I'm getting is I don't have the class Room.as. Would you mind posting that as well?

Note, that I am using 2.55 flixel.  What version are you using?
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: help with this dungeon generator problem
« Reply #5 on: Sat, Dec 31, 2011 »
package com 
{
   import org.flixel.*;
   /**
    * ...
    * @author
    */
   public class Room extends FlxObject
   {
      public var x2:uint;
      public var y2:uint;
      public var rect:FlxRect;
      
      public function Room(roomRect:FlxRect):void
      {
         x = roomRect.x;
         y = roomRect.y;
         x2 = x + roomRect.width;
         y2 = y + roomRect.height;
         rect = roomRect;
         
         
      }
      
      
      public function getCenter():FlxPoint
      {
         return new FlxPoint((x + x2) / 2, (y + y2) / 2);
      }
      
      public function intersects(other:Room):Boolean
      {
         return (this.x <= other.x2 && this.x2 >= other.x &&
                this.y <= other.y2 && this.y2 >= other.y)
      }
      
   }

}


Here is the Room class. I'm thinking the problem could be in the create room function..
Check out my game journal: http://alternatdev.wordpress.com

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: help with this dungeon generator problem
« Reply #6 on: Sat, Dec 31, 2011 »
Well, I've plugged in your room class finally.

However, I am getting none of the errors you specified.

I have structured my classes in which all the classes are in the same folder as the main class, so no going through folders like com and stuff.

However, I am not getting any noticable rooms. The only thing I'm getting is a full screen of just one tile. It might be because I'm not using your tilemap but I shouldn't need that.

So, anyways, here are the changes I made:

-You'll notice I changed the names on a couple of classes. That's only because to avoid some confusion in my own projects.

//Playstate
Code: [Select]
package
{
   import org.flixel.*;
   /**
    * ...
    * @author
    */
   public class TestDungeonAuto extends FlxState
   {
      public var testLevel:Level;
     
      override public function create():void
      {
         testLevel = new Level(100, 100, 20, 8);
         add(testLevel);
       
      }
     
    override public function update():void
{
super.update();

}
     
   }

}

//Level
Code: [Select]
package 
{
   import org.flixel.*;
   /**
    * ...
    * @author
    */
   public class Level extends FlxGroup
   {
      private const _tileWall:uint = 1;
      private const _tileEmpty:uint = 0;
      private var _tileMap:FlxTilemap;
      private var _tilesX:uint; //the number of tiles per row
      private var _tilesY:uint; //the number of tiles per column
      private var _maxRoomSize:uint; //the max width of each rooms
      private var _minRoomSize:uint; //the max height of each room
     
      private var _levelArray:Array;
      private var _finalArray:Array;
     
      private var _maxNumRooms:uint;
      private var _numRooms:uint;
      private var _rooms:Array;
      private var _tileSize:uint;
     
      //starting position of player
      public var playerX:uint;
      public var playerY:uint;
     
      [Embed(source = "pics/autotiles.png")] private var tileMapPNG:Class;

    //  [Embed(source = "pics/tileMap.png")] private const tileMapPNG:Class;
     
      public function Level(tilesX:uint,tilesY:uint,maxNumRooms:uint,tileSize:uint):void
      {
         _tilesX =tilesX;
         _tilesY = tilesY;
         _tileSize = tileSize;
         _maxNumRooms = maxNumRooms;
         _numRooms = 0;
         _minRoomSize = 6;
         _maxRoomSize = 10;
         
         
         generate_level();
         
         
         
         _tileMap = new FlxTilemap();
         _tileMap.loadMap(FlxTilemap.arrayToCSV(_finalArray, _tilesX), tileMapPNG, _tileSize, _tileSize);
         add(_tileMap);
         
      }
     
     
      public function generate_level():void
      {
         
         fillLevel();
         
         //trace(_levelArray);
         
         generateRooms();
         generateFinalMap();
                     
         
         
         
         
         
         
         
         
      }
     
     
     
     
      public function fillLevel():void
      {
         _levelArray = new Array();

         for (var xi:uint = 0; xi < _tilesX; xi++)
         {
            _levelArray.push(new Array);
            for (var yi:uint = 0; yi <_tilesY; yi++)
            {
               _levelArray[xi][yi]=_tileWall;
            }
         }

//FlxG.watch(_levelArray,"_levelArray","LevelArray");
      }
     
     
       
     
     
      public function generateRooms():void
      {
         _rooms = new Array();
         var room:DungeonRoom = new DungeonRoom();
         //
         createRoom(room);
         for (var i:uint = 0; i < _maxNumRooms; i++)
         {
            var w:uint =  _minRoomSize + Math.round(Math.random() * (_maxRoomSize-_minRoomSize));
            var h:uint = _minRoomSize + Math.round(Math.random() * (_maxRoomSize-_minRoomSize));
            var x:uint = Math.floor(Math.random() * _tilesX-_maxRoomSize-1);
            var y:uint = Math.floor(Math.random() * _tilesY-_maxRoomSize-1);
            trace(_levelArray.length);
         
         
            //var room:DungeonRoom = new DungeonRoom();
            createRoom(room);
           
         }         

         
      }
     
     
      public function createRoom(room:DungeonRoom):void
      {
         
         trace(_levelArray.length);
         
         for (var xi:uint = room.x+1; xi < room.x2; xi++)
         {
            for (var yi:uint = room.y+1; yi < room.y2; yi++)
            {
               _levelArray[xi][yi] = _tileEmpty;
            }
         }
         
         
      }     
     
     
      public function generateFinalMap():void
      {
         _finalArray = new Array();
         for (var yi:uint = 0; yi < _tilesY; yi++)
         {
            for (var xi:uint = 0; xi < _tilesX; xi++)
            {
               _finalArray.push(_levelArray[yi][xi]);
            }
         }
     
      }
         
   }

}

//Room
Code: [Select]
package 
{
   import org.flixel.*;
   /**
    * ...
    * @author
    */
   public class DungeonRoom extends FlxObject
   {
      public var x2:uint;
      public var y2:uint;
      public var rect:FlxRect;
     
      public function Room(roomRect:FlxRect):void
      {
         x = roomRect.x;
         y = roomRect.y;
         x2 = x + roomRect.width;
         y2 = y + roomRect.height;
         rect = roomRect;
         
         
      }
     
     
      public function getCenter():FlxPoint
      {
         return new FlxPoint((x + x2) / 2, (y + y2) / 2);
      }
     
      public function intersects(other:DungeonRoom):Boolean
      {
         return (this.x <= other.x2 && this.x2 >= other.x &&
                this.y <= other.y2 && this.y2 >= other.y)
      }
     
   }

}

Let me know how this works out for ya! Hope it helps some.
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

caladan21ph

  • Member
  • **
  • Posts: 51
  • Karma: +0/-0
    • View Profile
Re: help with this dungeon generator problem
« Reply #7 on: Tue, Jan 3, 2012 »
Thanks a lot, I'll try this out!
Check out my game journal: http://alternatdev.wordpress.com