I’m setting up a class of public static vars that are to be BitmapData.
The problem I’m having is I can’t seem to reference the vars dynamically in an array, making it impossible to give them values without some serious procdedural style coding.
This is what it looks like
package com.myPackage{
import flash.display.*;
import flash.events.*;
import flash.net.*;
public class Images extends Sprite{
private var url1:String = "http://www.mydomain.com/myImage.jpg";
private var url2:String = "http://www.mydomain.com/myImage2.jpg";
public static var IMAGE_ONE_BDATA:BitmapData;
public static var IMAGE_TWO_BDATA:BitmapData;
private var urlArray = new Array(url1,url2);
private var bDataArray = new Array(IMAGE_ONE_BDATA, IMAGE_TWO_BDATA);
private var count:int = 0;
public function Images(){
initLoader();
}
private function initLoader():void
{
if(count < urlArray.length){
var url:String = urlArray[count];
var loader:Loader = new Loader();
loader.load(new URLRequest(url));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function (e:Event):void{
bDataArray[count] = e.target.content.bitmapData;
trace(IMAGE_ONE_BDATA) // <-- this should trace ([object-bitmapdata]) but instead returns null.. this is where I have to make a direct reference like..
IMAGE_ONE_BDATA = e.target.content.bitmapData;
trace(IMAGE_ONE_BDATA) // <-- will now return my bitmapdata object
count++;
initLoader();
}else{
dispatchEvent(new ImagesLoadedEvent);
}
}
}
Should I assume it’s because my array is a private var and placing references to public static vars is illegal? Is somebody gonna call the flash police on me?
Any input highly appreciated.
-Jascha
You’re slightly mis-understanding what the variables are. They are not objects, they are references to objects.
Let me try to explain by walking you through your code:
So, these two variables are local only to the Images instance, and reference the two strings you defined:
Now you define two variables, but you don’t assign them any values, so for the moment they are empty, or in AS3 “null”:
Now you create a new array and pass into it the two objects referenced by url1 and url2. Note that you are not storing the two variables, you are storing the objects referenced by those variables. More of that in a minute:
so now, if you change url1 to “hello url1” and trace urlArray, you’ll still have the two urls in the array.
If all that makes sense, then hopefully you can see what will happen here:
you haven’t attached any objects to IMAGE_ONE_BDATA or IMAGE_TWO_BDATA; they are still null, so you’re passing null into the array twice. Later, if you assign an object to IMAGE_ONE_BDATA you haven’t assigned it to the array, just to that variable.
Now you’ve assigned the bitmapdata to the array, but not to IMAGE_ONE_BDATA. Your expectations are wrong, it should return null:
Next you do reference the bitmapData to IMAGE_ONE_BDATA, so now it does have a reference
The mistake is to think of IMAGE_ONE_BDATA as an object into which you put the bitmapData rather than as a name on a post-it note that you stick onto objects when you want to keep track of them. The bitmap-data itself is the object you’re interested in, not the name.
If you need your bitmap-data to be public and static, forget your IMAGE_ONE_BDATA and IMAGE_TWO_BDATA and define:
instead.
Hope this helps – no Flash police. 🙂