all groups > flash actionscript > march 2006 >
You're in the

flash actionscript

group:

referencing a variable throughout different timelines


Re: referencing a variable throughout different timelines Amy G
3/31/2006 10:59:46 AM
flash actionscript:

[quoted text, click to view]

SEB,

Using the trace() function is very important in troubleshooting.

Try putting in a
trace(n);
after your for loop.

What you will notice is n is undefined. Because you never instantiate the
variable n.

Try changing your code as follows (note: NOT TESTED)

var imagesToLoad:Array;
var loadImages:LoadVars = new LoadVars();
loadImages.onLoad = function() {
if (this.totalImages != undefined) {
imagesToLoad = new Array();
for (var i = 0; i < this.totalImages; i++) {
imagesToLoad[i] = this["img"+i];
_root.myLoader.theLoader.control["cProto"+i].onRelease =
function(){
_root.myLoader.theLoader.revolver.test.text =
imagesToLoad[i];
}
}
}

I am not sure what you are assigning with the imageToLoad[i] = this["img"+i]
line, but I will assume you know what you are doing.

This code could be changed in many ways. You could remove the imageToLoad
array altogether by assigning the test.text value = this["img+i], unless it
is an important array that you want to have global access to as you do here.

You can also use your value n, but you would want to loop through your
imagesToLoad array after your assignment loop.

HTH,
AB

referencing a variable throughout different timelines (_seb_)
3/31/2006 8:29:38 PM
I have the following code, which resides, in the timeline, at: _root.myLoader.theLoader.control:

var loadImages:LoadVars = new LoadVars();
loadImages.onLoad = function() {
if (this.totalImages != undefined) {
imagesToLoad = new Array();
for (var i = 0; i < this.totalImages; i++) {
imagesToLoad[i] = this["img"+i];
}
_root.myLoader.theLoader.control["cProto"+n].onRelease = function(){
_root.myLoader.theLoader.revolver.test.text = imagesToLoad[n];
}
}
}
var imagesToLoad:Array;

the dynamic text field "test" returns: "undefined" instead of the value of imagesToLoad[n].
Obviously my reference to imageToLoad[n] is not right. I don't understand from where I should target
that variable from within the onRelease function, so that the text field gets it in its own timeline.

I tried to reference it absolutely, e.g:

_root.myLoader.theLoader.revolver.test.text = _root.myLoader.theLoader.control.imagesToLoad[n];

But this does not work either.
I know it's guess work for you to tell what should be the actual path, but what I don't understand
is how to think of the actual path in this situation.

Everything works otherwise (that is, imagesToLoad is succesfully created as an array and contains
the right things). The only thing is I don't understand how to target the values of the array so
that they are passed to another timeline.

I hope you can help!

--
seb ( ---@webtrans1.com)
http://webtrans1.com | high-end web design
Re: referencing a variable throughout different timelines (_seb_)
3/31/2006 8:46:31 PM
so I did some trouble shooting, and I realized that if I replace:
_root.myLoader.theLoader.revolver.test.text = imagesToLoad[n];
with:
_root.myLoader.theLoader.revolver.test.text = imagesToLoad[0];

(I replace n with 0) the textfield test returns the right value.
So it is not the imagesToLoad array that is not passed trhough the timelines, but it is the n variable.

waht can I do to make sure the variable n is passed?

Here is the entire code again:

var loadImages:LoadVars = new LoadVars();
loadImages.onLoad = function() {
if (this.totalImages != undefined) {
imagesToLoad = new Array();
for (var i = 0; i < this.totalImages; i++) {
imagesToLoad[i] = this["img"+i];
}
_root.myLoader.theLoader.control["cProto"+n].onRelease = function(){
_root.myLoader.theLoader.revolver.test.text = imagesToLoad[n];
}
}
}
var imagesToLoad:Array;


I hope you can help!

--
seb ( ---@webtrans1.com)
http://webtrans1.com | high-end web design
Re: referencing a variable throughout different timelines (_seb_)
3/31/2006 10:00:10 PM
Thanks Amy, but n is instantiated right before the loadImages onLoad function, I omitted it in the code.

I understand now what is happening, but I don't know the solution:
I go through a loop through which n is incremented (n++).

At each step of the loop, I create a new MC ("cProto"+n), that is, cProto1, cProto2, etc.
And to each of these MC, I give the same function:

["cProto"+n].onRelease = function(){
_root.myLoader.theLoader.revolver.test.text = imagesToLoad[n];
}

The problem is that n, at the end of the loop, has a final value, and each one of the
imagesToLoad[n] takes this one final value, instead of 1,2,3 etc.

How can I resolve this?
Thanks so much for your help!

[quoted text, click to view]


--
seb ( ---@webtrans1.com)
http://webtrans1.com | high-end web design
AddThis Social Bookmark Button