Automatically including assets?

59 views
Skip to first unread message

Roméo Labonté

unread,
Jun 18, 2015, 6:43:25 PM6/18/15
to haxef...@googlegroups.com
Hello yes!
    Back when I started experimenting with HF, I did this to get assets in the game:

<assets path="assets">
    <image path="images/map.png" id="mapbg" />
    <sound path="sounds/qbit-sound.ogg" id="q-bit intro" />
    <text path="data/maps/maps.list" id="maps/maplist" />
    <font path="fonts/holobunnies.otf" id="fonts/holofont" />
</assets>

While this works well, the game I'm working on has grown large, and many assets need to be included. I've searched for a while but haven't been able to get an answer: is this the right way to do it? Is there a more practical way for large projects?

Thanks!

Chase de Languillette

unread,
Jun 18, 2015, 7:57:31 PM6/18/15
to haxef...@googlegroups.com
i made some simple classes that can read/write json to disk.  Here is the backbone of it - i think you could maybe leverage something similar to load assets?

public static function loadRaw(filePath:String):String
{
 
var path:haxe.io.Path = new haxe.io.Path(filePath);


 
var previousWorkingDirectory:String = Sys.getCwd();
 
Sys.setCwd(path.dir);
 
var fileContent:String = File.getContent(filePath);
 
Sys.setCwd(previousWorkingDirectory);


 
return fileContent;
}

public static function saveRaw(json:String, filePath:String):Void
{
 
var path:haxe.io.Path = new haxe.io.Path(filePath);
 sys
.FileSystem.createDirectory(path.dir);


 
var previousWorkingDirectory:String = Sys.getCwd();
 
Sys.setCwd(path.dir);
 
var fout = File.write(filePath, false);
 fout
.writeString(json);
 fout
.close();
 
Sys.setCwd(previousWorkingDirectory);


 trace
("saved file to " + filePath);
}

Roméo Labonté

unread,
Jun 19, 2015, 1:14:06 AM6/19/15
to haxef...@googlegroups.com
Very useful, thanks! Although, I would probably write something that generates the lines in the XML based on the file structure though, as I could simply drop assets there and use them based on that. I was wondering if there was something built into HF before I ventured into writing my own stuff!

Thanks again!

Ohmnivore

unread,
Jun 19, 2015, 4:30:39 PM6/19/15
to haxef...@googlegroups.com
I just do this:

<assets path="assets" />

then I can call FlxSprite(0, 0, "assets/images/kek.png");

or var fileContent:String = openfl.Assets.getText("assets/data/kek.txt");

Ohmnivore

unread,
Jun 19, 2015, 4:31:34 PM6/19/15
to haxef...@googlegroups.com

Roméo Labonté

unread,
Jun 21, 2015, 7:45:11 PM6/21/15
to haxef...@googlegroups.com
Exactly what I was looking for. Thanks! I feel a bit stupid for overlooking the OpenFL XML Format doc! Doh!
Reply all
Reply to author
Forward
0 new messages