Groups | Blog | Home
all groups > flash actionscript > march 2006 >

flash actionscript : Help simplifiying script


Amy G
3/6/2006 4:21:39 PM

[quoted text, click to view]

I may be mistaken, but it looks like you are setting up your onRealease for
the 9 buttons 9 times.

Not tested, but should be a starter:

for (k=1; k<10; k++) {
myMC:MovieClip = this.createEmptyMovieClip("button"+k, 110+k);
myMC.lineStyle(1, 0x000000, 100);
myMC._x = 55+(70*k);
myMC._y = 0;
myMC.beginFill(0xCCCCCC, 10);
myMC.moveTo(0, 0);
myMC.lineTo(0, 80);
myMC.lineTo(60, 80);
myMC.lineTo(60, 0);
myMC.endFill(0, 0);
activateButton(myMC, k);
}

function activateButton(mc:MovieClip, k:Number) {
mc.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard" + k + ".swf", pcardZone);
}
}

jkwilkerson
3/6/2006 11:06:18 PM
Fellow Flasher,

I have an issue. Can anyone take a look at the included script and give me an
idea of how to tidy it up? Basically I would like to know if there is a call or
function that I can do to condense the last nine button calls. I am creating
the "buttons" dynamically and I would like to assign the button function to
each button dynamically.



for (k=1; k<10; k++) {
this.createEmptyMovieClip("button"+k, 110+k);
this["button"+k].lineStyle(1, 0x000000, 100);
this["button"+k]._x = 55+(70*k);
this["button"+k]._y = 0;
this["button"+k].beginFill(0xCCCCCC, 10);
this["button"+k].moveTo(0, 0);
this["button"+k].lineTo(0, 80);
this["button"+k].lineTo(60, 80);
this["button"+k].lineTo(60, 0);
this["button"+k].endFill(0, 0);
button1.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard1.swf", pcardZone);
};
button2.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard2.swf", pcardZone);
};
button3.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard3.swf", pcardZone);
};
button4.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard4.swf", pcardZone);
};
button5.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard5.swf", pcardZone);
};
button6.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard6.swf", pcardZone);
};
button7.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard7.swf", pcardZone);
};
button8.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard8.swf", pcardZone);
};
button9.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard9.swf", pcardZone);
};
}
blemmo
3/7/2006 12:15:32 AM
You can assign the onRelease function in the for loop too, if you add a
variable to the buttons:
--
this.id = k;
this.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard"+this.id+".swf", pcardZone);
};
--
The variable id is necessary to remember the number,
loadMovie("pcard"+i+".swf") would read the actual value of "i" at the moment
the button is pressed.

hth,
blemmo
blemmo
3/7/2006 1:03:08 AM
Originally posted by: Newsgroup User
function activateButton(mc:MovieClip, k:Number) {
mc.onRelease = function() {
unloadMovie("pcardZone");
loadMovie("pcard" + k + ".swf", pcardZone);
}
}

That's a good way, that way you don't need an extra variable like proposed in
my earlier post. I tested it and it was working fine.

cheers,
blemmo

oh, Amy, could you please quote only the relevant parts of a post? Fullquotes
are a bit annoying with the time... thx ;)

AddThis Social Bookmark Button