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

flash actionscript : Help with onRelease event handler inside loop



squimscan
4/9/2005 12:00:00 AM
(sorry if this post is a dupe, I tried posting last night but it seems to have
disappeared)

I've been struggling with what ought to be a pretty simple event handler...

I have a Flash file with several buttons (named button1, button2, button3,
etc..); there are an equal number of movieclips (named movieclip1, movieclip2,
movieclip3, etc...). The movieclips start out hidden. When the user clicks on
any button, the corresponding movieclip should appear, and start playing (at
frame 1).

I'm going to have many Flash files, each with a different number of buttons
(and movie clips), so rather than hard-coding it for each instance, I want to
set it up so I can re-use the code, and just enter a variable for the number of
buttons in each .fla.

Simple enough, no? Well, here's how I'm trying to do it, but for reasons
which escape me (but which are probably immediately obvious to others), it
ain't working:

//begin code

number = 5;
// Number of buttons/movie clips. This variable will be different for each
flash file

for (i-1; i<=number; i++) {

["button"+i].onRelease = function () {
["movieclip"+i]._visble = true;
["movieclip"+i].gotoAndPlay(1);
}
}

// end code

I assume that the for loop runs once for each button, and that when you click
on any button, it should reveal the corresponding movieclip and start it
playing....but it's not working.

I seem to be missing something critical here. Can someone point me in the
right direction?

Thanks for your help!

PS: Running Flash MX, not MX 2004, if that matters.
Boonana
4/9/2005 12:00:00 AM
try
this["button"+i].onRelease = function () {
this["movieclip"+i]._visble = true;
this["movieclip"+i].gotoAndPlay(1);
NSurveyor
4/9/2005 8:15:15 PM
All the buttons will refer to the same movieclip. Eventually "i" will equal
"number", so all of them will point to the same movieclip. Where the onRelease
is called, it will use the variable "i", but that variable "i" will change, so
it won't work. Instead, create a variable for each button so you can find out
it's "i". Something like this:

this["button"+i].ivar = i;
this["button"+i].onRelease = function () {
this._parent["movieclip"+this.ivar]._visble = true;
this._parent["movieclip"+this.ivar].gotoAndPlay(1);
}

Or you could just put the movieclip in a variable:

this["button"+i].cmc = this["movieclip"+i];
this["button"+i].onRelease = function () {
this.cmc._visble = true;
this.cmc.gotoAndPlay(1);
}
NSurveyor
4/9/2005 8:20:09 PM
Jeckyl
4/10/2005 12:00:00 AM
[quoted text, click to view]

You've made up your own meaning for syntax here .. or rather, what you've
written means something completely different to what you think it does
because you have gotten confused about the correct syntax,

What YOU have written is creating an array with the string "button1" (say)
in it, and then put a onRelease method on that array (which does nothing).

Also

[quoted text, click to view]

You have i-1 instead of i = 1. Again, even though it is valid syntax, it is
not doing what you are wanting to do. It is just working out the value one
less than 'i' and then throwing that value away, whereas you most likely
want to set the value of 'i' to 1

Please check your code more thoroughly and read and learn more about arrays
and objects

Jeckyl

AddThis Social Bookmark Button