Quick help with loading map from 2D Array



  • Sorry I'm still a bit new with Flixel and for some reason reason I cannot get my map to appear on the screen. I don't really know what I'm doing wrong because most tutorials/questions asked about loading maps is used with .loadMap() and not .loadMapFrom2DArray or the other loads. I am not getting any errors from this, only a black screen when I load up the game I'm working on. Any help would be appreciated and I'll be willing to answer any questions you have about the code I have.

    Basically I have

    _map.loadMapFrom2DArray(mapData, AssetPaths.tiles2__png, 16, 16); 
    

    where mapData is:

    private var mapData:Array<Array<Int>> =  [[1,1,1,1,1,1,1,1],
    			                  [1,0,0,0,0,0,0,1],
    			                  [1,0,1,0,0,0,0,1],
    			                  [1,0,0,0,0,1,0,1],
    			                  [1,0,0,0,0,0,0,1],
    			                  [1,1,1,1,1,1,1,1]];
    

    Then the basic add(_map)


  • administrators

    Black screen on what target? On Flash, this is often a symptom for an exception you're not seeing because you're not using the Flash Debug Player.

    Have you tried Neko?



  • I do have flash debug player in my FlashDevelop and I am getting no errors when I run the program. As Neko is concerned when I try to run it I get a warning saying "export/windows/neko/haxe/ApplicationMain.hx:205: characters 57-75 : Warning : Use programPath instead", but I don't really know how to use Neko to be honest. Like I said I'm still new to all of this.



  • I notice your upperleft tile is 1. I could be totally remembering wrong, but I thought I read somewhere about that needing to be 0 for some reason.


  • administrators

    @IBwWG If that's the case, which I doubt, it'd be a bug.



  • I bought the book Discover HaxeFlixel and it loads a map from an array a little differently.

    First he declares the array like this, as a 1D array.

    private var mapData:Array<Int> = [
    1,1,1,1,1,1,1,1,                              
    1,0,0,0,0,0,0,1,
    1,0,1,0,0,0,0,1,
    1,0,0,0,0,1,0,1,
    1,0,0,0,0,0,0,1,
    1,1,1,1,1,1,1,1
    ];
    

    And then he uses the loadMapFromArray() method which takes 2 extra parameters than the loadMapFrom2DArray method.
    Lets say your level is 20 tiles across and 15 high then the method call would be:

    _map.loadMapFromArray(mapData, 20, 15, AssetPaths.tiles__png, 16, 16);
    

    Give it a try and see what you get.



  • Nope, tried the regular array and making the first one 0 and still the same problem.



  • Make sure that you have correct version of tileset in assets folder named tiles2.png.
    Make sure first two tiles in tileset are not black or transparent.
    Make sure each tile in tileset is 16x16 pixels wihout spacing.
    Try giving the tilemap position of (0, 0) and scrollFactor of (0, 0).
    Try enabling debug draw (http://haxeflixel.com/documentation/debugger) - do you see colored borders around tiles?



  • Made sure I had tiles2.png in my assets folder and it's there.
    Tiles2 is where i got rid of the first transparent block that tiles.png originally had.
    I made sure each tile is 16x16.
    Added the camera position and the map position to 0,0.
    Also added FlxG.debugger.drawDebug and the screen is still just black.

    Originally I had this set up with Ogmo and it worked fine, but I decided to not use that because I wanted to work on getting my levels generated by my own code and I can't even get them to appear.... I honestly can not figure out why they aren't loading properly. I am not getting any errors in my code at all. It's just completely black when I load the game.



  • @LordTakahiro
    Is _map declared as a FlxTilemap? I tried this and it works for me. Are you adding anything after add(_map) covering it up, like a background sprite or something? Maybe post more of your code, or try a minimal test project to eliminate other factors that may be causing this.

    private var _map:FlxTilemap;
    private var mapData:Array<Array<Int>> = [
    [1,1,1,1,1,1,1,1],
    [1,0,0,0,0,0,0,1],
    [1,0,1,0,0,0,0,1],
    [1,0,0,0,0,1,0,1],
    [1,0,0,0,0,0,0,1],
    [1,1,1,1,1,1,1,1]];	
    
    _map = new FlxTilemap();
    _map.loadMapFrom2DArray(mapData, AssetPaths.tiles2__png, 16, 16);
    add(_map);
    

    BTW, the tileset image I used has tile 0 that is transparent with no problem.



  • @dean said in Quick help with loading map from 2D Array:

    @LordTakahiro
    Is _map declared as a FlxTilemap? I tried this and it works for me. Are you adding anything after add(_map) covering it up, like a background sprite or something? Maybe post more of your code, or try a minimal test project to eliminate other factors that may be causing this.

    private var _map:FlxTilemap;
    private var mapData:Array<Array<Int>> = [
    [1,1,1,1,1,1,1,1],
    [1,0,0,0,0,0,0,1],
    [1,0,1,0,0,0,0,1],
    [1,0,0,0,0,1,0,1],
    [1,0,0,0,0,0,0,1],
    [1,1,1,1,1,1,1,1]];	
    
    _map = new FlxTilemap();
    _map.loadMapFrom2DArray(mapData, AssetPaths.tiles2__png, 16, 16);
    add(_map);
    

    BTW, the tileset image I used has tile 0 that is transparent with no problem.

    OMG thank you. I did have to declared as a tilemap but I did not add the _map = new FlxTilemap(); out of stupidity! haha. That is all it was >.< now I feel silly. I have what I was working on working now. just from that simple _map = new FlxTilemap() x.x



  • @LordTakahiro

    Glad you got it working!


  • administrators

    @LordTakahiro Hm.. So it was a classical null reference error? That should've been easy to debug. Did you not see an exception pop up in your flash debug player?



  • @Gama11 no not at all. I had no errors pop up at all. I even had the game's main menu load but when i hit play it was just blank.


  • administrators

    Are you sure you were using the flash debug player? That's the except symptoms of using the regular flash player, which swallows exceptions and tries to carry on.


Log in to reply