Duplicate class field declaration



  • source/AssetPaths.hx:3: characters 1-7 : Duplicate class field declaration : player__png
    source/AssetPaths.hx:4: characters 0-19 : Defined in this class

    On different folders of the assets folder I have images called player.png. It is giving this error. How can I prevent it?

    One is in assets/images/player

    and the other in assets/levels/tiles/characters

    http://puu.sh/xucVu/463dc9e0ee.png



  • @agustín-pérez-fernández Does it work when you try renaming Player.json to Player1.json?



  • i don't think you can. as far as i know, assets are referenced by name so having two files with the same name will throw off the file reference builder and return that error you're getting

    a workaround would be to place those files on a different directory under your source (for ex: "yourGame/assets2/"), which unless you add it to the list on AssetPaths.hx, won't be picked up by it and won't throw you any errors. you can then manually reference your files by doing this instead of using AssetPaths:

    var PLAYER_FILES:String = "assets2/";
    player = new FlxSprite(PLAYER_FILES + "playerOne/player.png");
    player2 = new FlxSprite(PLAYER_FILES + "playerTwo/player.png");
    

    you do have to remember to add that folder to your project.xml file, but that's easy:

    <assets path="assets2" embed="true"/> <!-- use false here if you don't want to embed those files -->
    

    i reference most assets this way in my game, it's a bit more error-prone but it's handy and faster when you have 300 files and AssetPaths gets a bit slow


  • administrators

    Another alternative is to have multiple AssetPaths classes for different directories.

    @:build(flixel.system.FlxAssets.buildFileReferences("assets/images", true))
    class Images {}
    
    @:build(flixel.system.FlxAssets.buildFileReferences("assets/sounds", true))
    class Sounds {}
    
    @:build(flixel.system.FlxAssets.buildFileReferences("assets/music", true))
    class Music {}
    

    You get the idea.



  • Thanks for the replies and solutions!

    I like zkylon solution. Whath for is the AssetPaths class needed? It is only to have the assets accessible by code autocomplete or it has another function?


  • administrators

    @agustín-pérez-fernández said in Duplicate class field declaration:

    It is only to have the assets accessible by code autocomplete or it has another function?

    Nope, it's only for convenience and auto-completion. It also has the nice side effect of producing a compiler error if an asset doesn't exist (got deleted / moved / whatever). With String paths, you'd only notice that at runtime.



  • @gama11 said in Duplicate class field declaration:

    Another alternative is to have multiple AssetPaths classes for different directories.

    @:build(flixel.system.FlxAssets.buildFileReferences("assets/images", true))
    class Images {}
    
    @:build(flixel.system.FlxAssets.buildFileReferences("assets/sounds", true))
    class Sounds {}
    
    @:build(flixel.system.FlxAssets.buildFileReferences("assets/music", true))
    class Music {}
    

    You get the idea.

    hadn't realized this, that's really useful and will definitely implement it in my game like today. my main gripe with assetpaths is that once you have a ton of files autocomplete gets pretty slow, but if u can filter it into smaller classes that's really handy.

    btw you can also do the following:

    @:build(flixel.system.FlxAssets.buildFileReferences("assets", true, ["png", "jpg"]))
    class Images {}
    
    @:build(flixel.system.FlxAssets.buildFileReferences("assets", true, ["mp3"]))
    class Music {}
    
    

    it's still smart to have things separate on different folders but that's another good use

    also gama's last point about string paths is important, be wary that if you change a filename or extension and don't update it on your code the only way you'll notice is when it crashes. use string paths sparingly, use AssetPaths as much as you can

    cheers!


Log in to reply
 

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