Results 1 to 3 of 3

Thread: How to simplify this simple code?

  1. #1
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question How to simplify this simple code?

    i have code below
    Code:
    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
    Code:
    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
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

  2. #2
    Join Date
    Oct 2006
    Location
    New York, NY, USA
    Posts
    245
    Thanks
    39
    Thanked 24 Times in 24 Posts

    Default Not SIMPLE but CONDENSED

    For my non-programming brain, something like the following code I snitched (or that posted at http://www.dynamicdrive.com/forums/s...d.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.



    Code:
    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›
    Last edited by auntnini; 07-19-2011 at 01:53 AM. Reason: spelling

  3. #3
    Join Date
    Aug 2008
    Location
    karanganyar, solo, indonesia
    Posts
    161
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Thanks so much for reply, i will try deep based on your post,
    ///////////////////////////////////////////////////
    ///// http://www.mediatutorial.web.id
    ///////////////////////////////////////////////////

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •