Problem to use TMX maps into

109 views
Skip to first unread message

Clayton Verlique

unread,
Nov 5, 2015, 6:04:23 PM11/5/15
to HaxeFlixel
Hello guys...

I have a problem to use maps created in Tile Map Editor into HaxeFlixel... I heard that is good use TMX beacause u can put the collisions... but the flash still show ERROR.

This is my codes:

package;

import flixel.addons.editors.ogmo.FlxOgmoLoader;
import flixel.addons.editors.tiled.TiledMap;
import flixel.FlxCamera;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.ui.FlxButton;
import flixel.util.FlxColor;
import flixel.util.FlxMath;
import flixel.tile.FlxTilemap;
import haxe.io.Path;
import openfl.Assets;

/**
 * A FlxState which can be used for the actual gameplay.
 */
class PlayState extends FlxState
{
private var player:FlxSprite;
private var map:TiledLevel;
/**
* Function that is called up when to state is created to set it up. 
*/
override public function create():Void
{
super.create();
map = new TiledLevel("assets/Map/map.tmx");
add(map.floorTiles);
add(map.collideTiles);
add(map.overTiles);
//-----------------------------------------------------------------------------------------------------------------------

add(new FlxText(0, 0, 100, "Play State"));
FlxG.camera.bgColor = 0xFF0080FF;
//setCamera();
player = new Player(0, 0);
add(player);
}
function setCamera():Void
{
FlxG.camera.follow(player, FlxCamera.STYLE_PLATFORMER);
FlxG.camera.setBounds(0, 0, 0, 0, true);
}
/**
* Function that is called when this state is destroyed - you might want to 
* consider setting all objects this state uses to null to help garbage collection.
*/
override public function destroy():Void
{
super.destroy();
}

/**
* Function that is called once every frame.
*/
override public function update():Void
{
super.update();
}
}

And:

package;

import openfl.Assets;
import haxe.io.Path;
import haxe.xml.Parser;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.group.FlxGroup;
import flixel.tile.FlxTilemap;
import flixel.addons.editors.tiled.TiledMap;
import flixel.addons.editors.tiled.TiledObject;
import flixel.addons.editors.tiled.TiledObjectGroup;
import flixel.addons.editors.tiled.TiledTileSet;

/**
 * ...
 * @author Verlique
 */
class TiledLevel extends TiledMap
{
        // Para cada "Tile Layer" no mapa, deve ser definido a propriedade "tileset" uqe ontem o nome da imagem do Tile sheet 
        private inline static var pathLevelTiles = "assets/data/";
        
        // Array de tilemaps usado para colisoes
        public var floorTiles:FlxGroup;
        public var collideTiles:FlxGroup;               
        public var overTiles:FlxGroup;
        private var collidableTileLayers:Array<FlxTilemap>;
        
        public function new(tiledLevel:Dynamic)
        {
                
                floorTiles = new FlxGroup();
                collideTiles = new FlxGroup();                
                overTiles = new FlxGroup();
                
                FlxG.camera.setBounds(0, 0, fullWidth, fullHeight, true);
                
                // Carregar Tile Maps
                for ( tileLayer in layers )
                {
                        var tileSheetName:String = tileLayer.properties.get("tileset");
                        
                        if (tileSheetName == null)
                                throw "'tileset' property not defined for the '" + tileLayer.name + "' layer. Please add the property to the layer.";
                                
                        var tileSet:TiledTileSet = null;
                        for ( ts in tilesets )
                        {
                                if ( ts.name == tileSheetName)
                                {
                                        tileSet = ts;
                                        break;
                                }
                        }
                        
                        if (tileSet == null)
                                throw "Tileset '" + tileSheetName + " not found. Did you mispell the 'tilesheet' property in " + tileLayer.name + "' layer?";
                                
                        var imagePath           = new Path(tileSet.imageSource);
                        var processedPath       = pathLevelTiles + imagePath.file + "." + imagePath.ext;
                        
                        var tilemap:FlxTilemap = new FlxTilemap();
                        tilemap.widthInTiles = width;
                        tilemap.heightInTiles = height;
                        tilemap.loadMap(tileLayer.tileArray, processedPath, tileSet.tileWidth, tileSet.tileHeight, 0, 1, 1, 1);
                        
                        if (tileLayer.properties.contains("nocollide"))
                        {
                                if(tileLayer.properties.contains("over"))
                                        overTiles.add(tilemap);

                               floorTiles.add(tilemap);
                        }
                        else
                        {
                                if (collidableTileLayers == null)
                                        collidableTileLayers = new Array<FlxTilemap>();
                                
                                collideTiles.add(tilemap);
                                collidableTileLayers.push(tilemap);
                        }
                }
                super(tiledLevel);
        }
        
        public function collideWithLevel(obj:FlxObject, ?notifyCallback:FlxObject->FlxObject->Void, ?processCallback:FlxObject->FlxObject->Bool):Bool
        {
                if (collidableTileLayers != null)
                {
                        for ( map in collidableTileLayers)
                        {
                                // Sempre colidir com objetos
                                return FlxG.overlap(map, obj, notifyCallback, processCallback != null ? processCallback : FlxObject.separate);
                        }
                }
                return false;
        }
}

When I go to the flash appears this:


TypeError: Error #1009: Cannot access a property or method of a null object reference.
 at
TiledLevel()[C:\Development\EngineOn\source\TiledLevel.hx:42]
 at
PlayState/create()[C:\Development\EngineOn\source\PlayState.hx:32]
 at flixel
::FlxGame/switchState()[C:\HaxeToolkit\haxe\lib\flixel\3,3,11\flixel\FlxGame.hx:583]
 at flixel
::FlxGame/update()[C:\HaxeToolkit\haxe\lib\flixel\3,3,11\flixel\FlxGame.hx:668]
 at flixel
::FlxGame/step()[C:\HaxeToolkit\haxe\lib\flixel\3,3,11\flixel\FlxGame.hx:648]
 at flixel
::FlxGame/onEnterFrame()[C:\HaxeToolkit\haxe\lib\flixel\3,3,11\flixel\FlxGame.hx:493]


Keith Weatherby II

unread,
Nov 6, 2015, 1:13:35 AM11/6/15
to HaxeFlixel
go to TiledLevel.hx line 42 where it says


for ( tileLayer in layers )

and change it to:
for ( tileLayer in tiledLevel.layers )

It's possible there are other errors in your code, but this should get you partially there

Also read this tutorial (In English so you may have to figure it out on your own) :
http://coinflipstudios.com/devblog/?p=182

Clayton Verlique

unread,
Nov 6, 2015, 11:09:51 AM11/6/15
to haxef...@googlegroups.com
This line starting giving a error when I change..

source/TiledLevel.hx:43: characters 23-40 : You can't iterate on a Dynamic value, please specify Iterator or Iterable

I tried so many times that tutorial, but the errors keep coming :/

----------------------------------- EDITED ----------------------------------------

When I try follow that tutorial appears this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at flixel.tile::FlxTilemap/loadMap()[C:\HaxeToolkit\haxe\lib\flixel\3,3,11\flixel\tile\FlxTilemap.hx:459]
at PlayState/create()[C:\Development\EngineOn\source\PlayState.hx:91]
at flixel::FlxGame/switchState()[C:\HaxeToolkit\haxe\lib\flixel\3,3,11\flixel\FlxGame.hx:583]
at flixel::FlxGame/update()[C:\HaxeToolkit\haxe\lib\flixel\3,3,11\flixel\FlxGame.hx:668]
at flixel::FlxGame/step()[C:\HaxeToolkit\haxe\lib\flixel\3,3,11\flixel\FlxGame.hx:648]
at flixel::FlxGame/onEnterFrame()[C:\HaxeToolkit\haxe\lib\flixel\3,3,11\flixel\FlxGame.hx:493]

This Line:

            level.loadMap(layer.tileArray, tilesheetPath, tileSize, tileSize, FlxTilemap.OFF, tileGID);

Keith Weatherby II

unread,
Nov 7, 2015, 12:00:32 PM11/7/15
to HaxeFlixel
Sorry I'm not an expert, however the tutorial works fine because I'm using that same code in my game.

I don't know what to say, other than do the tutorial from scratch in a completely new project, so you're not carrying any old bugs.

Once you enter it *as written* then try to get tha working, when you have that working then go back to your own code and figure out what you left out.

I realize you may have went through the tutorial, but I don't think you typed in everything he said in there because it does work.
Reply all
Reply to author
Forward
0 new messages