Welcome, Guest. Please login or register.
Did you miss your activation email?
02/07/12, 09:18
Home Help Search Login Register
News: Parsley Flex framework review featuring quiz application, in our Flex frameworks series
Flex SDK 4.5 mobile roadmap: begin with your mobile development
Swiz Flex framework review featuring quiz application
New homepage we release our new Homepage, take a look ...

+  Flash-db
|-+  General
| |-+  Flash 8 (Moderators: Jorge Solis, ..:: Mazhar Hasan ::.., Andresss)
| | |-+  Variable scope access???
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Print
Author Topic: Variable scope access???  (Read 1345 times)
dc2698
Server what's that
*
Posts: 9


View Profile Email
« on: 10/21/09, 01:39 »

My code:

Code:
onClipEvent (construct)
{
function isLoaded(success)
{
if (!success)
{
return (0);
} // end if
objArray = new Array();
var pic = this.firstChild.attributes.pic;
objArray.push(pic);
                          trace(objArray[0]);  // NO PROBLEM!!!
} // End of the function
var xmlDoc = new XML();
xmlDoc.ignoreWhite = true;
xmlDoc.onLoad = isLoaded;
xmlDoc.load("pic.xml");
trace(objArray[0]);  // UNDEFINED!?!?!?!?!?!??!?!?!?
    img_source = objArray[0];  // BIG PROBLEM
}

My pic.xml is just <?xml version="1.0" encoding="utf-8" standalone="yes" ?><items pic="img01.jpg"></items>

What made me mad is that, objArray[0] can be accessed only in the isLoaded(). When trying to access it outside isLoaded() then it failed.

Please help me out thanks in advance!
« Last Edit: 10/21/09, 06:58 by dc2698 » Logged
Ronald Wernecke
Administrator
Systems Administrator
*****
Posts: 6161


View Profile WWW Email
« Reply #1 on: 10/21/09, 03:54 »

This is the way it is designed.
If you define a variable inside of a function, it is private to the function.

You could put a _global inf front of it, and the it is a global variable accessible by all.

Or put a _root up front, and then it is accessible through the root MC - or whatever you like to reside it Wink

Be aware - this will change with later versions.
Logged

happy flashing
Cool
Ronald
dc2698
Server what's that
*
Posts: 9


View Profile Email
« Reply #2 on: 10/21/09, 07:07 »

Thanks for your tip.

Can you please show me how to make objArray global?  Accessable by img_source outside isLoaded()?
Logged
Ronald Wernecke
Administrator
Systems Administrator
*****
Posts: 6161


View Profile WWW Email
« Reply #3 on: 10/21/09, 09:49 »

_global.objArray=new Array();
defines a global variable - but you have to access it the same way with _global.variableName.
This way all the clips know this variable.
As I said: this works until AS3 Wink
Logged

happy flashing
Cool
Ronald
dc2698
Server what's that
*
Posts: 9


View Profile Email
« Reply #4 on: 10/21/09, 11:16 »

I am sorry but outside isLoaded(), trace(_global.objArray[0]); still undefined and nothing was assigned to img_source..................is there any other way please?
Logged
Jorge Solis
Administrator
Systems Administrator
*****
Posts: 14593


View Profile
« Reply #5 on: 11/03/09, 06:34 »

Since the variable is a XML loading callback, you should be sure that the XML is fully loaded before tring to access it. Also using onClipEvent (deprecated since Flash 5) is not a good idea

Jorge
Logged

Pages: [1] Print 
« previous next »
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!