Groups | Blog | Home
all groups > flash actionscript > june 2005 >

flash actionscript : Can't access arrays created in a function


Anil Natha
6/16/2005 3:58:33 PM
[quoted text, click to view]

If a variable is declared as global you can access it from anywhere
without using the _global keyword.

HOWEVER:

When you attempt to change the value of a _global variable, i believe in
that instance you have to use the _global keyword.

This is what I've experienced when I've used the _global keyword.

Typically though for better programming practices it's better to stay
away from global variables from what I remember being taught in school.

Dave Mennenoh
6/16/2005 5:50:47 PM
I believe you need to use the _global each and every time you access the
variable.

--
Dave -
www.blurredistinction.com/director
www.macromedia.com/go/team

raylaur
6/16/2005 10:36:22 PM
I have created arrays from a loaded XML file. I verified they were there by
tracing from within the function. Trouble is I can't call them from outside of
the function. Here is a piece of code used to create the array:

When I try to get an item from an item using 'thisCue[n] somewhere else in the
code it comes up undefined (the n has been given a value). I'm stumped.



var vlist:XML = new XML();
vlist.load("demovars_en2.xml");
vlist.ignoreWhite = true;
vlist.onLoad = function(success) {
if (success) {
var videoClip =
vlist.firstChild.childNodes[0].childNodes[0].childNodes[0].nodeValue;
var vCuePoints:Array = vlist.firstChild.childNodes[1].childNodes;
_global.thisCue = new Array();
for (i=0; i<vCuePoints.length; i++) {
thisCue[i] = vCuePoints[i].childNodes[0].nodeValue;
}
mandingo
6/16/2005 11:11:29 PM
Things may be different on MX04 than just Flash MX but in the latter, if you
declare a var like that inside a function, you are telling it to be a temporary
local variable for the function. Once the function has finished, the variable
no longer exists.

either create the array outside the function and reference the changes to the
array or don't var the item which will allow it to persist outside the duration
of the function call.

cheers,
Jeckyl
6/17/2005 12:00:00 AM
[quoted text, click to view]

He didn't create the array var with 'var' .. he used _global prefix .. so it
won't be local, and that won't be the problem.
--
Jeckyl

Jeckyl
6/17/2005 12:00:00 AM
[quoted text, click to view]

You're welcome. Only trying to help :)

Roger

Jeckyl
6/17/2005 12:00:00 AM

Are you sure that code is executed BEFORE you try to access the array?

Have you verified the value of 'n' in 'thisCue[n]' is valid?

Have you traced out the entire array (eg trace(thisCue))?

Have you any other variables called thisCue that may be masking the global..
just in case, it is best to always use '_global.thisCue' instead of just
'thisCue'. It is very easy to accidentally mask the global variable from
being seen. eg. look at the little example:
_global.x = 10;
x ++;
The x++ actually create a timeline variable called x which masks the global
x .. so elsewhere in the program ,when you refer to the global x value, it
will still be 10. Bugs like that are easy to miss. SO best to avoid them
by always putting '_global.' prefix on all global variables.
--
Jeckyl

raylaur
6/17/2005 12:15:42 AM
Mandingo: I changed to the push method and these arrays are still undefined
outside of the onLoad function where they were created.

Jeckyl:

[quoted text, click to view]

Yes. it's part of an onLoad function at the top of the
page. I'm accessing the array outside of the function later on.

[quoted text, click to view]

Yes. And I also tried index numbers in the brackets.
It works in the function but not out.

[quoted text, click to view]

Yes. Within the For loop it traces out fine - the
whole array is there.

[quoted text, click to view]
just in case,Text

No. And I am using the _global prefix in each case


mandingo
6/17/2005 12:23:46 AM
when you say you are accessing the array later on... how much later on?

If it is still on the same frame but further down the code, then Jeckyl could
well be right... the onLoad hasn't completed creating the array before you are
trying to reference it... in your onload, put a trace in after the array is
finished,
trace("thisCue array is ready");
then before the area you are trying to reference, trace("about to access
thisCue"); and see if they fire in the correct order.
raylaur
6/17/2005 12:48:16 AM
Mandingo, Jeckyl - you were right. I put the traces after the array push and
before the array access later on. It tried to access the array before the
array was done.

How would you suggest 'delaying' until the arrays are populated? Is there a
way to test it's done before trying to access?

Thanks...
mandingo
6/17/2005 1:01:05 AM
one of two methods...

put your onLoad on frame one and the rest of the code on Frame 2... then the
frame one code will finish and then the frame 2...

the other would be to wrap all your substantive code in a function and call
the function at the end of the onLoad(success) so that after it is successful
it calls the function to initialise all the other code.

cheers,

oh and Jeckyl thanks about the _global thing... but I had already picked up on
that and corrected that error...
raylaur
6/17/2005 1:34:14 AM
Mandingo - that worked. Thanks!

One more quick question. I want to check the video time at intervals and use
it as a cuepoint event to drive other actions.
I thought I could store it in a variable but it comes out undefined outside of
the function...any tips for using video time to drive actions?

function getvTime() {

_global.vTime = ns.time;
}
setInterval(getvTime, 100);
AddThis Social Bookmark Button