Skip to main content
Participating Frequently
November 29, 2007
Question

simplifying button script

  • November 29, 2007
  • 4 replies
  • 333 views
this is the script for 24 buttons that play 24 movie clips (its a chronological timeline with thumbnails and popups). The second line calls a function to clear all other buttons than the one clicked, by going to a frame "hidden" in each movie clip. The 3rd is fine. All works. Is there a much shorter way of writing this ie arrays for the button names and the movie clips. I 'm just curious to learn by simplifying the script.


thumb_1_btn.onRelease = function() {
_parent._parent.turnOff();
_root.popUp_1_mc.gotoAndPlay("showing");
};
thumb_2_btn.onRelease = function() {
_parent._parent.turnOff();
_root.popUp_2_mc.gotoAndPlay("showing");
};
thumb_3_btn.onRelease = function() {
_parent._parent.turnOff();
_root.popUp_3_mc.gotoAndPlay("showing");

..... and on up to 24
This topic has been closed for replies.

4 replies

Inspiring
November 29, 2007
bedtime read! you're gonna fall asleep in minutes!
4pixelsAuthor
Participating Frequently
November 29, 2007
Brilliant. Thank you both. Am going to print this out for a little bedtime read.
Inspiring
November 29, 2007
for (i=1;i<25;i++) {
this["thumb_" + i +"_btn"].onRelease = function() {
_parent._parent.turnOff();
_root["popUp_"+ i +"_mc"].gotoAndPlay("showing");
};
}
Inspiring
November 29, 2007
Catboris123,

> Is there a much shorter way of writing this ie arrays for the
> button names and the movie clips.

You betcha. If your array holds actual instance names, then the
following would do:

var buttons:Array = [thumb_1_btn, thumb_2_btn];
for (var i:Number = 0; i < buttons.length; i++) {
buttons .onRelease = function():Void {
// button code here
}
}

If your array holds strings, you would have to convert your string into
an object reference with eval() or the array access operator, as shown here:

http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically

Of course, for buttons named in squence like this, you could get away
without an array at all. In that case, you'd have to use the eval()/array
access operator approach to convert your strings:

for (var i:Number = 1; i < 25; i++) {
this["button_" + i + "_btn"].onRelease = function():Void {
// button code here
}
}

Note that in the last example, the for() loop counts from 1 rather than
zero. Arrays start at zero, but your instance names start from 1.


David Stiller
Co-author, Foundation Flash CS3 for Designers
http://tinyurl.com/2k29mj
"Luck is the residue of good design."