PDA

View Full Version : How could I tidy this script up?


cannon303
11-13-2009, 10:32 AM
Hi I'm new to learning AS3 and it's about as enjoyable as gargling on broken glass. But hey I'm trying to get with the times, so here is my script, I have nine buttons and essentially I'm using the same function apart from the values are different and therefore I'm having to write out a separate function for each button. I'll show you the script for two buttons and you'll see that the only changes are values from 1 to 2, so is there a way I can write just one function for all of the buttons and just pass the relevant value to it via a variable?

root[main].thumb1.addEventListener(MouseEvent.CLICK, onButton1Click);
root[main].thumb2.addEventListener(MouseEvent.CLICK, onButton2Click);


function onButton1Click(evt:MouseEvent):void {
if(whoseClicked !=1){

mc1=new transitionClick1();
mc1.name = "mc1";
mc1.tranA.inner.stagePic.addChild(bigImagesA[1]);
mc1.tranB.inner.stagePic.addChild(bigImagesB[1]);
addChild(mc1);
mc1.x=57.8;
mc1.y=39.9;
root[main].transition1.gotoAndStop(1);
root[main].transition1.tranA.inner.masker.gotoAndStop(1);
root[main].transition1.tranA.inner.gotoAndStop(1);
root[main].transition1.tranA.alpha=0;
root[main].transition1.tranB.inner.masker.gotoAndStop(1);
root[main].transition1.tranB.inner.gotoAndStop(1);
root[main].transition1.tranB.alpha=0;
root[main].stop();
whoseClicked =1;
}
}

function onButton2Click(evt:MouseEvent):void {
if(whoseClicked !=2){

mc2=new transitionClick2();
mc2.name = "mc2";
mc2.tranA.inner.stagePic.addChild(bigImagesA[2]);
mc2.tranB.inner.stagePic.addChild(bigImagesB[2]);
addChild(mc2);
mc2.x=57.8;
mc2.y=39.9;
root[main].transition2.gotoAndStop(1);
root[main].transition2.tranA.inner.masker.gotoAndStop(1);
root[main].transition2.tranA.inner.gotoAndStop(1);
root[main].transition2.tranA.alpha=0;
root[main].transition2.tranB.inner.masker.gotoAndStop(1);
root[main].transition2.tranB.inner.gotoAndStop(1);
root[main].transition2.tranB.alpha=0;
root[main].stop();
whoseClicked =2;
}
}

I used to be able to pass the variable inside the brackets at the end of each function name but the (evt:MouseEvent) gets in the way


Any ideas?

Ta

cannon303
11-13-2009, 12:25 PM
I've sorted it thanks