A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Generalizing function

  1. #1
    Senior Member sakie's Avatar
    Join Date
    Apr 2005
    Location
    London
    Posts
    285

    Generalizing function

    Hi

    I'm usingfollowing script to create a toggle button (I know its a weirdly written script but this is the best I could do not being a coder )

    Actionscript Code:
    /**************          Button-1 ******************/
    bltrBtn1.addEventListener(MouseEvent.ROLL_OVER, manageMouseOver1, false, 0, true);
    bltrBtn1.addEventListener(MouseEvent.ROLL_OUT, manageMouseOut1, false, 0, true);
    bltrBtn1.addEventListener(MouseEvent.MOUSE_DOWN, manageMouseDown1, false, 0, true);
    bltrBtn1.addEventListener(MouseEvent.CLICK, manageMouseClick1, false, 0, true);

    function manageMouseOver1(event:MouseEvent):void
    {
        event.target.MovieClip
        if (bltrBtn1.currentFrame == 1)
        {
            bltrBtn1.gotoAndStop('over');
           
        }
        else if (bltrBtn1.currentFrame == 5)
        {
            bltrBtn1.gotoAndStop('overAndSelected');
        }
    }

    function manageMouseOut1(event:MouseEvent):void
    {
        if (bltrBtn1.currentFrame == 2)
        {
            bltrBtn1.gotoAndStop('up');
        }
        else if (bltrBtn1.currentFrame == 6)
        {
            bltrBtn1.gotoAndStop('upAndSelected');
        }
    }

    function manageMouseDown1(event:MouseEvent):void
    {
        if (bltrBtn1.currentFrame == 2)
        {
            bltrBtn1.gotoAndStop('down');
        }
        else if (bltrBtn1.currentFrame == 6)
        {
            bltrBtn1.gotoAndStop('downAndSelected');
        }
    }

    function manageMouseClick1(event:MouseEvent):void
    {
        if (bltrBtn1.currentFrame == 3)
        {
            bltrBtn1.gotoAndStop('overAndSelected');
        }
        else if (bltrBtn1.currentFrame == 7)
        {
            bltrBtn1.gotoAndStop('over');
        }
    }

    I've 12 other buttons just like bltrBtn1 how can I generalize this code so that all of my buttons can use the same functions as they're exactly same and I don't see the point in copying pasting same code for 12 times .

    Many thanks in advance..

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You apparently started down the right path, but didn't get very far. You need to use the target or currentTarget property of the event to get the object which dispatched the event or is listening for it, respectively. In this case, they should be the same, but I generally prefer to use currentTarget.

    Code:
    function manageMouseOver(event:MouseEvent):void{
        var btn:MovieClip = MovieClip(event.currentTarget);
        if (btn.currentFrame == 1){
            btn.gotoAndStop('over');
        }else if (btn.currentFrame == 5){
            btn.gotoAndStop('overAndSelected');
        }
    }
    
    function manageMouseOut(event:MouseEvent):void{
        var btn:MovieClip = MovieClip(event.currentTarget);
        if (btn.currentFrame == 2){
            btn.gotoAndStop('up');
        } else if (bltrBtn1.currentFrame == 6){
            btn.gotoAndStop('upAndSelected');
        }
    }
    
    function manageMouseDown(event:MouseEvent):void{
        var btn:MovieClip = MovieClip(event.currentTarget);
        if (btn.currentFrame == 2){
            btn.gotoAndStop('down');
        }else if (bltrBtn1.currentFrame == 6){
            btn.gotoAndStop('downAndSelected');
        }
    }
    
    function manageMouseClick(event:MouseEvent):void{
        var btn:MovieClip = MovieClip(event.currentTarget);
        if (btn.currentFrame == 3){
            btn.gotoAndStop('overAndSelected');
        } else if (btn.currentFrame == 7){
            btn.gotoAndStop('over');
        }
    }
    I don't like the use of frame numbers for semantic purposes. Could you not set properties to define whatever special meaning frames 1,2,3,5,6 and 7 have? Or at least set descriptive labels and check those instead of numbers. Of course, this also implies you are using frames for state rather than animation, which is unfortunately extremely common in flash. To the point where it's just "how it's done".

    Extra credit: Notice that all your functions look pretty much identical, except for the numbers and labels used. We can abstract out the commonality into a function that creates our listener functions.

    Code:
    /**************          Button-1 ******************/
    bltrBtn1.addEventListener(MouseEvent.ROLL_OVER, makeListener(1, 'over', 5, 'overAndSelected'), false, 0, true);
    bltrBtn1.addEventListener(MouseEvent.ROLL_OUT, makeListener(2, 'up', 6, 'upAndSelected'), false, 0, true);
    bltrBtn1.addEventListener(MouseEvent.MOUSE_DOWN, makeListener(2, 'down', 6, 'downAndSelected'), false, 0, true);
    bltrBtn1.addEventListener(MouseEvent.CLICK, makeListener(3, 'overAndSelected, 7, 'over'), false, 0, true);
    
    function makeListener(onFrame:int, goLabel:String, elseFrame:int, elseLabel:String):Function{
      return function(event:MouseEvent):void{
         var btn:MovieClip = MovieClip(event.currentTarget);
         if (btn.currentFrame == onFrame){
           btn.gotoAndStop(goLabel);
        }else if (btn.currentFrame == elseFrame){
           btn.gotoAndStop(elseLabel);
        }
      }
    }

  3. #3
    Senior Member sakie's Avatar
    Join Date
    Apr 2005
    Location
    London
    Posts
    285
    Many thanks .. that is exactly what I was looking for .. you're a star

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center