Skip to main content
Participating Frequently
August 24, 2009
Question

Global Variable within function

  • August 24, 2009
  • 1 reply
  • 812 views

I'm having an issue with a global variable. I'm trying to give it a value within a function and it isn't working.

The variable is imageList. It's an XML List. I'm making it global so I can use it anywhere in my project. I suppose if someone has a better idea to avoid the global variable, I'm up for that too. But for now, it seems like the way to go.

Here's my .as file:

package {     public class MyGlobal     {           public static var imageList:XMLList;     } }

And here is where I'm trying to add a value to it:

function ParseImages(imageInput:XML):void{      trace ("parsing images");      MyGlobal.imageList = images.gallery.image;            test_txt.text = (MyGlobal.imageList[0]);

I have import MyGlobal in the code above this and I've tried putting it in the function as well.

"text_txt.text = (MyGlobal.imageList[0]);" does work. It outputs what I want, but if I move this outside ParseImages, it gives me the error: "TypeError: Error #1009: Cannot access a property or method of a null object reference"

And here's where I want to access the new value. This is within a movieclip on the same frame as the code above:

import MyGlobal; // var l = new Loader();      l.load(new URLRequest('Photography/'+ MyGlobal.imageList[0]));

It's giving me the same same Error #1009 as above. It seems like a scope problem, but why? Shouldn't it work since it's a global variable? What am I doing wrong?

Thanks in advance for your help.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
August 24, 2009

are you trying to use that variable before it's defined?  use trace() functions if you don't understand to see which executes first -  defining your variable or trying to use your variable.

Participating Frequently
August 24, 2009

Hey, thanks for the quick reply! No, I shouldn't be trying to use it before it is defined. Here's the code:

//php section import flash.net.URLLoader; import flash.display.Loader; import flash.events.*; import flash.net.*; import MyGlobal; var images:XML; var myRequest:URLRequest; var myLoader:URLLoader; // list of image urls that will come from loaded XML MyGlobal.imageList; myRequest = new URLRequest("Photography_1.php"); myLoader = new URLLoader(); myLoader.addEventListener(Event.COMPLETE, onFileLoaded); // suggested handler for unexpected errors - avoids some headaches myLoader.addEventListener(IOErrorEvent.IO_ERROR, onLoadError); myLoader.load(myRequest); // Note: all the listeners are removed // it is always wise to remove listeners that are needed any longer // to make objects eligible for arbage collection function onLoadError(e:IOErrorEvent):void {      trace(e.toString());      myLoader.removeEventListener(Event.COMPLETE, onFileLoaded);      myLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError); }           function onFileLoaded(e:Event):void {      trace ("File Loaded");      myLoader.removeEventListener(Event.COMPLETE, onFileLoaded);      myLoader.removeEventListener(IOErrorEvent.IO_ERROR, onLoadError);            images = new XML(myLoader.data);      // only now xml is ready and you can start loading images       trace(images) ParseImages(images); } function ParseImages(imageInput:XML):void{      trace ("parsing images");      MyGlobal.imageList = images.gallery.image;            test_txt.text = (MyGlobal.imageList[0]); //play movieclip img_Loader.gotoAndPlay(1); }

I'm defining the variable in function ParseImages and then using it within the img_Loader movie clip, which shouldn't even play until the end of that same function. The "text_txt.text = (MyGlobal.imageList[0];" outputs exactly what I want to access later in the movieclip so I know the value is being added. It just seems to disappear after the function ParseImages.

I had assumed it should work since it's global, but I'm not sure what the problem is. I'm new to classes and global variables in as3 so I could be doing something wrong; I'm just not sure what.

kglad
Community Expert
Community Expert
August 24, 2009

are you trying to reference that list on the first frame of your img_Loader?