Asset Management
So far we have been using assets in a very simple way. In order to
optimize the way you work with assets, it is highly recommend using an
Assets
object working as a central
location to get resources from. This Assets
object would be responsible for pooling
the assets used and make sure they are reused instead of being thrown
away and instanciated again which could pressure the GC.
In this code, we define a getTexture
API on the Assets
object, allowing us to retrieve an
embedded texture:
public
static
function
getTexture
(
name
:
String
)
:
Texture
{
if
(
Assets
[
name
]
!=
undefined
)
{
if
(
sTextures
[
name
]
==
undefined
)
{
var
bitmap
:
Bitmap
=
new
Assets
[
name
]();
sTextures
[
name
]
=
Texture
.
fromBitmap
(
bitmap
);
}
return
sTextures
[
name
];
}
else
throw
new
Error
(
"Resource not defined."
);
}
Note here, the use of a simple Dictionary
, to store the asset, so that the
next time we need, we just grab it from the pool, instead of recretating
it.
As usual, the textures are embedded through the Embed
tag:
[
Embed
(
source
=
"../media/textures/background.png"
)]
private
static
const
Background
:
Class
;
Note that we have been using the Embed tag since the beginning but
Starling does not force you to use embedded textures; you can also load
a texture dynamically by using a Loader
object for instance:
// create the Loader
var
loader
:
Loader
=
new
Loader
();
// listen to the Event.COMPLETE event
loader
.
contentLoaderInfo
.
addEventListener
(
Event
.
COMPLETE
,
onComplete
);
// load the image
loader
.
load
(
new
URLRequest ...
Get Introducing Starling now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.