Unexpected Private



  • I'm following the tutorial on the website but I have a problem at step 6 - Loading the Tilemap:
    as i try to build the project it notifies me an error in this line of code:

    private function placeEntities(entityName:String, entityData:Xml):Void

    it says: "Unexpected private"

    I tried changing private to public and simply erase it, and moving around the function definition (but still inside " override public function create():Void ")



  • @Lirija

    you're probable missing a } right before the line giving the error.



  • Seems like it's not that (this time)

    package;

    import flixel.FlxG;
    import flixel.FlxObject;
    import flixel.FlxSprite;
    import flixel.FlxState;
    import flixel.addons.editors.ogmo.FlxOgmoLoader;
    import flixel.text.FlxText;
    import flixel.tile.FlxTilemap;
    import flixel.ui.FlxButton;
    import flixel.math.FlxMath;

    class PlayState extends FlxState
    {
    private var _player:Player;
    private var _map:FlxOgmoLoader; //For Tiled, change FlxOgmoLoader to TiledMap
    private var _mWalls:FlxTilemap;

    override public function create():Void
    {
    	_map = new FlxOgmoLoader(AssetPaths.room_001__oel);
    	_mWalls = _map.loadTilemap(AssetPaths.tiles__png, 16, 16, "walls");
    	_mWalls.follow();
    	_mWalls.setTileProperties(1, FlxObject.NONE);
    	_mWalls.setTileProperties(2, FlxObject.ANY);
    	add(_mWalls);
    
    	private function placeEntities(entityName:String, entityData:Xml):Void
    	{
    			var x:Int = Std.parseInt(entityData.get("x"));
    			var y:Int = Std.parseInt(entityData.get("y"));
    			if (entityName == "player")
    			{
    				_player.x = x;
    				_player.y = y;
    			}
    	}
    	
    	_player = new Player();
    	_map.loadEntities(placeEntities, "entities");
    	add(_player);
    
    	super.create();
    }
    
    override public function update(elapsed:Float):Void
    {
    	super.update(elapsed);
    }
    

    }



  • This post is deleted!


  • Just remove the private part.
    Local functions can't have access modifiers iirc.



  • @DleanJeans
    I deleted my post because it wasn't of any help. I'm not sure of the local functions. Don't you still need to pass the args in to placeEntities() though?
    _map.loadEntities(placeEntities, "entities");



  • @DleanJeans
    I tried but that generates more random errors;

    @dean
    Yes actually that makes sense to me but i've just followed the tutorial really:


    Now, we need to make our player object get placed in the right location on the map. So, change where we initialize our player from:

    _player = new Player(20, 20);
    to:

    _player = new Player();
    _map.loadEntities(placeEntities, "entities");
    We're simply telling our _map object to loop through all of the entities in our 'entities' layer, and call the placeEntities() for each one (which we're about to make now).

    TiledMap does not have a built-in loadEntities() function, so for that you'll need to change this to:

    var tmpMap:TiledObjectLayer = cast _map.getLayer("entities");
    for (e in tmpMap.objects)
    {
    placeEntities(e.type, e.xmlData.x);
    }
    Let's make the placeEntities() function now. When we call loadEntities() on our map, it will pass the name of the entity, as well as its XML data to whatever function we want. In our function, we need to take this information and do something with it. It will look like this now:

    private function placeEntities(entityName:String, entityData:Xml):Void
    {
    var x:Int = Std.parseInt(entityData.get("x"));
    var y:Int = Std.parseInt(entityData.get("y"));
    if (entityName == "player")
    {
    _player.x = x;
    _player.y = y;
    }
    }
    So, if this function gets passed an entity with the name "player", it will set our player object's x and y values to the entity's x and y values (converting them from String to Int).


    and i don't know what i am supposed to pass anyway.



  • REFORMATTED:

    @DleanJeans
    I tried but that generates more random errors;

    @dean
    Yes actually that makes sense to me but i've just followed the tutorial really:


    Now, we need to make our player object get placed in the right location on the map. So, change where we initialize our player from:

    _player = new Player(20, 20);
    

    to:

    _player = new Player();
    _map.loadEntities(placeEntities, "entities");
    

    We're simply telling our _map object to loop through all of the entities in our 'entities' layer, and call the placeEntities() for each one (which we're about to make now).

    TiledMap does not have a built-in loadEntities() function, so for that you'll need to change this to:

    var tmpMap:TiledObjectLayer = cast _map.getLayer("entities");
    for (e in tmpMap.objects)
    {
        placeEntities(e.type, e.xmlData.x);
    }
    

    Let's make the placeEntities() function now. When we call loadEntities() on our map, it will pass the name of the entity, as well as its XML data to whatever function we want. In our function, we need to take this information and do something with it. It will look like this now:

    private function placeEntities(entityName:String, entityData:Xml):Void
    {
         var x:Int = Std.parseInt(entityData.get("x"));
         var y:Int = Std.parseInt(entityData.get("y"));
         if (entityName == "player")
         {
             _player.x = x;
             _player.y = y;
         }
      }
    

    So, if this function gets passed an entity with the name "player", it will set our player object's x and y values to the entity's x and y values (converting them from String to Int).


    and i don't know what i am supposed to pass anyway.



  • @dean said in Unexpected Private:

    @DleanJeans
    I deleted my post because it wasn't of any help. I'm not sure of the local functions. Don't you still need to pass the args in to placeEntities() though?
    _map.loadEntities(placeEntities, "entities");

    No, since the first parameter of loadEntities() is a function:

    loadEntities(EntityLoadCallback:String‑>Xml‑>Void, EntityLayer:String = "entities"):Void
    


  • Yeah you need to move that placeEntities() function out of create():

    override public function create():Void
    {
    	_map = new FlxOgmoLoader(AssetPaths.room_001__oel);
    	_mWalls = _map.loadTilemap(AssetPaths.tiles__png, 16, 16, "walls");
    	_mWalls.follow();
    	_mWalls.setTileProperties(1, FlxObject.NONE);
    	_mWalls.setTileProperties(2, FlxObject.ANY);
    	add(_mWalls);
    	
    	_player = new Player();
    	_map.loadEntities(placeEntities, "entities");
    	add(_player);
    
    	super.create();
    }
    
    private function placeEntities(entityName:String, entityData:Xml):Void
    {
    	var x:Int = Std.parseInt(entityData.get("x"));
    	var y:Int = Std.parseInt(entityData.get("y"));
    	if (entityName == "player")
    	{
    		_player.x = x;
    		_player.y = y;
    	}
    }
    

    You should check the source code for the tutorial if you get stuck:



  • Oh thanks! I hadn't realized i could do that.



  • @DleanJeans The problem was that i was trying to define a function inside a method right?


  • administrators

    @Lirija While that's not the intended way to do it in the tutorial, Haxe still supports nested functions like that.



  • @Gama11 Then i just don't understand why i had that error when i tried to define the function inside create(); i defined the function (without any access specifiers ) just before the call in _map.loadEntities (but it seems that Haxe doesn't really care where I choose to define a function nor it is needed to declare it anywhere)


  • administrators

    @Lirija What was the error you had after "Unexpected private"? I think you just said:

    I tried but that generates more random errors;

    Here's a simple example for a nested function:

    http://try-haxe.mrcdk.com/#a7289

    class Test {
        static function main() {
            function nestedFunction(string:String) {
                trace(string);
            }
        
            nestedFunction("Hello World");
        }
    }
    

Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.