The Assets class is a wrapper around openfl.utils.Assets that provides safe access to asset loading functions with Funkin-specific caching.
Overview
The Assets class provides methods for loading:
- Text files and binary data
- Images and bitmap data
- Sound effects and music
- Asset libraries
All methods are static and can be called directly without instantiation.
Properties
The OpenFL assets cache. Access directly for cache management.
Example:
// Clear specific asset from cache
Assets.cache.removeBitmapData("assets/images/menuBG.png");
// Check if asset is cached
if (Assets.cache.hasBitmapData("assets/images/character.png"))
{
trace("Character sprite is cached");
}
Methods
getPath()
Returns the file system path for an asset.
public static function getPath(path:String):String
The asset path relative to the assets folder.
Returns: The absolute path to the asset on the file system.
Example:
var fullPath = Assets.getPath("assets/data/introText.txt");
trace(fullPath); // e.g., "C:/Games/FunkinGame/assets/data/introText.txt"
Text and Binary Assets
getText()
Loads text from an asset synchronously.
public static function getText(path:String):String
The asset path to load from.
Returns: The text contents of the file.
Synchronous loading may cause stutters. Use loadText() for asynchronous loading when possible.
Example:
// Load intro text synchronously
var introText = Assets.getText("assets/data/introText.txt");
trace(introText);
loadText()
Loads text from an asset asynchronously.
public static function loadText(path:String):Future<String>
The asset path to load from.
Returns: A Future that resolves with the text contents.
Example:
import openfl.utils.Future;
// Load text asynchronously
Assets.loadText("assets/data/dialogue.txt").onComplete(function(text:String)
{
trace("Loaded dialogue: " + text);
processDialogue(text);
});
getBytes()
Loads binary data from an asset synchronously.
public static function getBytes(path:String):haxe.io.Bytes
The asset path to load from.
Returns: The byte contents of the file.
Example:
// Load binary data
var saveData = Assets.getBytes("assets/data/save.dat");
loadBytes()
Loads binary data from an asset asynchronously.
public static function loadBytes(path:String):Future<openfl.utils.ByteArray>
The asset path to load from.
Returns: A Future that resolves with the byte contents.
Image Assets
getBitmapData()
Loads a bitmap image synchronously.
public static function getBitmapData(path:String, useCache:Bool = true):openfl.display.BitmapData
The asset path to load from.
Whether to use the asset cache. Set to false to force a fresh load.
Returns: The loaded BitmapData.
Synchronous loading may cause stutters. Use loadBitmapData() for asynchronous loading when possible.
Example:
// Load character sprite
var charSprite = Assets.getBitmapData("assets/images/characters/bf.png");
// Load without caching (for memory management)
var tempImage = Assets.getBitmapData("assets/images/temp.png", false);
loadBitmapData()
Loads a bitmap image asynchronously.
public static function loadBitmapData(path:String):Future<openfl.display.BitmapData>
The asset path to load from.
Returns: A Future that resolves with the loaded BitmapData.
Example:
// Load image asynchronously
Assets.loadBitmapData("assets/images/menuBG.png").onComplete(function(bmp:BitmapData)
{
var sprite = new FlxSprite();
sprite.loadGraphic(bmp);
add(sprite);
});
Audio Assets
getSound()
Loads a sound file synchronously.
public static function getSound(path:String):openfl.media.Sound
The asset path to load from.
Returns: The loaded Sound object.
Synchronous loading may cause stutters. Use loadSound() for asynchronous loading when possible.
Example:
// Load sound effect
var confirmSound = Assets.getSound("assets/sounds/confirmMenu.ogg");
confirmSound.play();
loadSound()
Loads a sound file asynchronously.
public static function loadSound(path:String):Future<openfl.media.Sound>
The asset path to load from.
Returns: A Future that resolves with the loaded Sound.
Example:
// Load sound asynchronously
Assets.loadSound("assets/sounds/explosion.ogg").onComplete(function(sound:Sound)
{
sound.play();
});
getMusic()
Loads a music file synchronously with optimizations for long-duration audio.
public static function getMusic(path:String):openfl.media.Sound
The asset path to load from.
Returns: The loaded Sound object optimized for music.
Example:
// Load background music
var bgMusic = Assets.getMusic("assets/music/freakyMenu.ogg");
FlxG.sound.playMusic(bgMusic);
loadMusic()
Loads a music file asynchronously with optimizations for long-duration audio.
public static function loadMusic(path:String):Future<openfl.media.Sound>
The asset path to load from.
Returns: A Future that resolves with the loaded music Sound.
Example:
// Load and play music asynchronously
Assets.loadMusic("assets/music/breakfast.ogg").onComplete(function(music:Sound)
{
FlxG.sound.playMusic(music);
});
Asset Library Management
exists()
Checks whether an asset exists.
public static function exists(path:String, ?type:openfl.utils.AssetType):Bool
The asset type to check (IMAGE, SOUND, TEXT, etc.).
Returns: True if the asset exists.
Example:
if (Assets.exists("assets/images/custom.png", IMAGE))
{
trace("Custom image exists!");
}
else
{
trace("Using default image");
}
list()
Retrieves a list of all assets of a given type.
public static function list(?type:openfl.utils.AssetType):Array<String>
The asset type to list. If omitted, lists all assets.
Returns: Array of asset paths.
Example:
// List all image assets
var images = Assets.list(IMAGE);
for (img in images)
{
trace("Found image: " + img);
}
// List all sound assets
var sounds = Assets.list(SOUND);
trace("Total sounds: " + sounds.length);
hasLibrary()
Checks if an asset library exists.
public static function hasLibrary(name:String):Bool
The library name to check.
Returns: True if the library exists.
getLibrary()
Retrieves an asset library by name.
public static function getLibrary(name:String):lime.utils.AssetLibrary
The name of the library to get.
Returns: The AssetLibrary object.
loadLibrary()
Loads an asset library asynchronously.
public static function loadLibrary(name:String):Future<openfl.utils.AssetLibrary>
The name of the library to load.
Returns: A Future that resolves with the AssetLibrary.
Example:
// Load a custom asset library
Assets.loadLibrary("week7").onComplete(function(lib:AssetLibrary)
{
trace("Week 7 assets loaded!");
// Now week7 assets are available
});
Best Practices
Use Asynchronous Loading
Prefer async methods to avoid frame stutters:
// Bad: Synchronous loading causes stutter
var image = Assets.getBitmapData("assets/images/large.png");
// Good: Asynchronous loading
Assets.loadBitmapData("assets/images/large.png").onComplete(function(bmp)
{
// Use image here
});
Check Asset Existence
Validate assets exist before loading:
var assetPath = "assets/data/custom.json";
if (Assets.exists(assetPath, TEXT))
{
var data = Assets.getText(assetPath);
}
else
{
trace("Asset not found: " + assetPath);
}
Manage Cache
Manually manage cache for memory-intensive assets:
// Load without cache for temporary use
var tempBitmap = Assets.getBitmapData("assets/images/temp.png", false);
use(tempBitmap);
tempBitmap.dispose();
// Or remove from cache when done
var cachedBitmap = Assets.getBitmapData("assets/images/cached.png");
use(cachedBitmap);
Assets.cache.removeBitmapData("assets/images/cached.png");
Example: Asset Preloader
import funkin.Assets;
import openfl.utils.Future;
class AssetPreloader
{
public static function preloadAssets(onComplete:Void->Void):Void
{
var toLoad = [
"assets/images/menuBG.png",
"assets/images/menuDesat.png",
"assets/music/freakyMenu.ogg"
];
var loaded = 0;
var total = toLoad.length;
function checkComplete():Void
{
loaded++;
trace('Loaded ${loaded}/${total} assets');
if (loaded >= total)
{
onComplete();
}
}
// Load images
Assets.loadBitmapData(toLoad[0]).onComplete(function(_) checkComplete());
Assets.loadBitmapData(toLoad[1]).onComplete(function(_) checkComplete());
// Load music
Assets.loadMusic(toLoad[2]).onComplete(function(_) checkComplete());
}
}
See Also