Dynamic File Loading

so I’m making a rhythm game where the players can create custom maps and add custom songs (json, mp3, png for background), but i don’t know how to load files which weren’t present during the build.

1 Like

https://api.openfl.org/openfl/net/URLLoader.html This API embodies one cross platform solution.

does it also work with local files?

For local files you can use

ByteArray.loadFromFile() which in the background is similar to URLLoader’s logic, or if you’re not using HTML/JS or Flash, Haxe’s File.getBytes(). Whichever suits your needs, there are several methods to achieve the same results.

I recommend async loading of data for multiple reasons even in desktop/mobile apps.

Yes, it works for local the local filesystem as well.

thanks it worked with the json files but how do i do it for .ogg and .png files?

For audio and image files, OpenFL has static methods Sound.fromFile( filepath ) and BitmapData.fromFile( filePath ).

but how do i use BitmapData.fromFile(); on FlxSprites?

Sorry, I wasn’t aware that you were using HaxeFlixel.

I’m not familiar with HaxeFlixel, but by looking at the API, it looks like you can one of the following:

var bitmapData:BitmapData = BitmapData.fromFile( srcPath );
myFlxSprite.graphics = FlxGraphic.fromBitmapData(bitmapData);

myFlxSprite.pixels = BitmapData.fromFile( srcPath );

as indicated by these API references below:

thanks it worked

even tho i had to do

var mySprite = new FlxSprite(0, 0, bitmapData);

now i just have to find out how to do it with audio files (FlxSound)

Maybe this?

var byteArray:ByteArray = ByteArray.fromFile(soundSrcPath);

loadByteArray() only works on flash11 (i build to windows / linux)

ok i got it by doing this:

var musicData:ByteArray = ByteArray.fromFile(audioSrc);

var music = new Sound();
music.loadCompressedDataFromByteArray(musicData, musicData.length);


thanks you guys helped me a lot!