开发者

Actionscript 3 Reference to public static var in array not working

开发者 https://www.devze.com 2022-12-21 17:17 出处:网络
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 valu

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 w开发者_StackOverflow社区hat 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:

private var url1:String = "http://www.mydomain.com/myImage.jpg";
private var url2:String = "http://www.mydomain.com/myImage2.jpg";

Now you define two variables, but you don't assign them any values, so for the moment they are empty, or in AS3 "null":

public static var IMAGE_ONE_BDATA:BitmapData;
public static var IMAGE_TWO_BDATA:BitmapData;

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:

private var urlArray = new Array(url1,url2);

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:

private var bDataArray = new Array(IMAGE_ONE_BDATA, IMAGE_TWO_BDATA);

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:

    bDataArray[count] = e.target.content.bitmapData;
    trace(IMAGE_ONE_BDATA) // <-- this _should_ trace null

Next you do reference the bitmapData to IMAGE_ONE_BDATA, so now it does have a reference

     IMAGE_ONE_BDATA = e.target.content.bitmapData;
     trace(IMAGE_ONE_BDATA) // <-- will now return my bitmapdata object

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:

public static var bDataArray:Array

instead.

Hope this helps - no Flash police. :)


Ok, this is what I'm going to try to keep some sort of reference structure going that I'm comfortable with.

outside the loop I will rename bDataArray to bDataNameArray. and declare another public static var array bDataObjects.

then inside the loop

var bDataNameArray[i]:Object = new Object();
bDataNameArray[i].bData = e.target.content.bitmapData;
bDataObjects.push(bDataNameArray[i]);

So now, perhaps if that works, further down the line in my code I can make references like

var bitmap:Bitmap = new Bitmap(Images.bDataObjects.IMAGE_ONE_DATA.bData);

or I can just stick to a conventional name for the objects. like ob:Object = new Object()... but how boring is that?

-J


Sadly AS3 doesn't allow you to make an object like that. You could always do this though:

public static var bDataNameArray:Object = new Object();
bDataNameArray["MY_AWESOME_UPPERCASE_NAME"] = e.target.content.bitmapData;

which stores the bitmap data into bDataNameArray. Then you could get it out by doing this somewhere else:

var data:BitmapData = Images.bDataNameArray["MY_AWESOME_UPPERCASE_NAME"];
var bitmap:Bitmap = new Bitmap(data);

Hope this all helps. Good luck.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号