For example, right now I have a class called "Balls.as". Here I load 10 different balls-images. You know, like this:
[Embed(source = "/ball1.png")]
[Embed(source = "/ball2.png")]
The problem is that if I spawn 5 balls, these balls images gonna get embeded 5 * 5 times right? Correct me if I'm wrong! So I though, can't I have a ballimageloading class? That loads these images once, and then in Balls.as I can load whatever ball i want for the moment?
Add one or more classes to the image by adding class=“INSERTCLASSOPTIONHERE” to the image's HTML code. For example: If you want to add multiple classes to an image, for example percent-25 and float-left, separate the classes by a space. For example:
According to what I have found, this is nowhere as easy to do in AS3 as it was in previous versions. One method that worked was to create a MovieClip to hold the image, then create an instance of that MovieClip and add it to the stage.
Percent classes can be applied in multiples of 5 from "percent-5" to "percent-95". Upload the image, add an alt tag, and insert your image into the Body field. Click Edit HTML above the Body field. In the HTML Editor, locate the image source code.
The source code has been updated to work around that restriction, but have left the example code intact since it will eventually work fine. In Part 1 of my Intro to AS3 Workers series I looked at the fundamentals of AS3 workers, including the various communication methods, and showed an example of a simple Hello World worker.
You won't need to "load" them, they are embedded. You just have to instantiate the images. It's a good idea to have one class that manage shared ressources, as such:
public class TextureAssets
{
[Embed(source = "../lib/ball1.png")]
private static var Ball1:Class;
[Embed(source = "../lib/ball2.png")]
private static var Ball2:Class;
public static var ball1Texture:BitmapData;
public static var ball2Texture:BitmapData;
public static function init():void
{
ball1Texture = (new Ball1() as Bitmap).bitmapData;
ball2Texture = (new Ball2() as Bitmap).bitmapData;
}
Then you would call TextureAssets.init()
once (in the Main.as for example)
and when you need the bitmapData: use new Bitmap(TextureAssets.ball1Texture)
This way your program needs uses only memory required for one bitmapData instead of having many which ends up being the same.
If you need to perform operations on a bitmapData while keeping the original you can use:
var modified:bitmapData = TextureAssets.ballTexture.clone();
Also, if you are instantiating all balls images from within one class it's a good idea to drop static access and instead initialise the bitmapDatas in the constructor, create a new TextureAssets() and call the textures through the variable
(Static field access is slower than direct (.) access : http://jacksondunstan.com/articles/1690)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With