PDA

View Full Version : How to simplify this simple code?



smansakra
07-12-2011, 11:38 AM
i have code below

btn_home.addEventListener(MouseEvent.CLICK, goto_home);
btn_contact.addEventListener(MouseEvent.CLICK, goto_contact);
btn_about.addEventListener(MouseEvent.CLICK, goto_about);

function goto_home(event:MouseEvent):void{
this.gotoAndStop(2);
}
function goto_contact(event:MouseEvent):void{
this.gotoAndStop(3);
}
function goto_about(event:MouseEvent):void{
this.gotoAndStop(4);
}

What i want to know is how to simplify above code like below


var myframe:int;
btn_home.addEventListener(MouseEvent.CLICK, gotoframe(2));
btn_contact.addEventListener(MouseEvent.CLICK, gotoframe(3));
btn_about.addEventListener(MouseEvent.CLICK, gotoframe(4));

function gotoframe(myframe):void{
this.object_okie.gotoAndStop(myframe);
}

UNFORTUNATELY that code doesn't work anymore.
can somebody help me?

IT WILL BE SO HARD FOR ME IF I MAKE 10 buttons, thanks

auntnini
07-19-2011, 01:45 AM
For my non-programming brain, something like the following code I snitched (or that posted at http://www.dynamicdrive.com/forums/showthread.php?t=62808;) is complex but condensed. It takes some forethought naming buttons, frame labels, SWFs, etc. so that each has a common denominator you can abstract and concatenate to take advantage of the event "target" or "current target" (which is also the object that registers and/or triggers the EventListener).

There is no way (to my knowledge) you can avoid having each object register its own specific EventListener. But notice below that three of them all reference the same function callMe(e:MouseEvent) -- which uses clever concatenation + e.currentTarget.name + to make the distinctions. There could also be differences in Event.TYPE in the EventListener (e.g., MouseEvent.CLICK, MouseEvent.MOUSE_UP, MouseEvent.MOUSE_DOWN) that could be sorted out by if/else or switch/case statements within a single referenced function.





Scene 1
Actions for Frame 1
black_mc.visible = false;
black_mc.buttonMode = true;
var myURL:URLRequest = new URLRequest("pages/blank.swf");
var myLoader:Loader = new Loader();
myLoader.load(myURL);
addChild(myLoader);
stage.addEventListener(Event.ENTER_FRAME, scrolling);
function scrolling(e:Event):void
{
if (black_mc.visible == false)
{
wall_mc.x += ((stage.stageWidth)/2 - mouseX)/5;
if(wall_mc.x < (wall_mc.width - stage.stageWidth)*-1)
{
wall_mc.x = (wall_mc.width - stage.stageWidth)*-1;
} else if(wall_mc.x > 0)
{
wall_mc.x = 0
}
}
}
wall_mc.me.addEventListener(MouseEvent.CLICK, callMe);
wall_mc.war.addEventListener(MouseEvent.CLICK, callMe);
wall_mc.sale.addEventListener(MouseEvent.CLICK, callMe);

function callMe(e:MouseEvent):void
{
myURL = new URLRequest("pages/" + e.currentTarget.name + ".swf");
myLoader.load(myURL);
black_mc.visible = true;
}
black_mc.addEventListener(MouseEvent.CLICK, loadBlank);
function loadBlank(e:MouseEvent):void
{
myURL = new URLRequest("pages/blank.swf");
black_mc.visible = false;
myLoader.load(myURL);
}
blank, ‹black_mc›
wall, ‹wall_mc›

smansakra
07-19-2011, 06:09 AM
Thanks so much for reply, i will try deep based on your post,