Skip to main content

FlixelAsset

View source

interface

org.flixelgdx.asset.FlixelAsset

public interface FlixelAsset<T>

Unified handle for one asset, with reference counting and lifecycle policy.

All assets retrieved from FlixelAssetManager implement this interface. FlixelGraphic implements FlixelAsset<FlixelGraphic> directly so the graphic object is the handle. Other asset types use FlixelDefaultAsset.

Call FlixelAsset.retain() when you take ownership of an asset handle and FlixelAsset.release() when you are done. The asset manager uses the reference count to decide when it is safe to unload the underlying data during state switches.

Typical usage in a sprite class:

// In a loading state.
Flixel.assets.load("player.png");

// In a game state.
FlixelAsset<FlixelGraphic> asset = Flixel.assets.get("player.png");
asset.retain();
FlixelGraphic graphic = asset.get();

// In destroy().
asset.release();

Methods

getPath()

public String getPath()

Returns the normalized asset path this handle was created for.

Returns: The asset path; never null.


get()

public T get()

Returns the content for this asset, loading it synchronously if it is not ready yet.

Prefer loading assets ahead of time in a loading state via FlixelAssetManager.load(String) and FlixelAssetManager.update() to avoid mid-frame stalls caused by a synchronous load.

Returns: The asset content; never null.


isLoaded()

public boolean isLoaded()

Returns true if the content for this asset is already available without triggering a synchronous load.

Returns: true if FlixelAsset.get() can return immediately without blocking.


isPersist()

public boolean isPersist()

Returns whether this asset survives FlixelAssetManager.clearNonPersist() when its reference count is zero.

Returns: true if persistent.


setPersist(boolean)

public FlixelAsset<T> setPersist(boolean persist)

Sets the persist flag for this asset. When true, the asset is kept in the manager cache across state switches even when its reference count is zero.

Parameters:

NameDescription
persisttrue to keep this asset across state switches when unreferenced.

Returns: this for chaining.


getRefCount()

public int getRefCount()

Returns the current reference count. Zero means no owner holds this asset.

Returns: The reference count.


retain()

public FlixelAsset<T> retain()

Increments the reference count. Call this when you take ownership of the asset.

Returns: this for chaining.


release()

public FlixelAsset<T> release()

Decrements the reference count. Call this when you are done with the asset. When the count reaches zero, FlixelAssetManager.onAssetReleased(FlixelAsset<?>) is called so the manager can apply the active FlixelAssetMode.

Returns: this for chaining.