dcsimg
A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: function parameters and addEventListner for simple button

  1. #1
    Senior Member
    Join Date
    Aug 2000
    Location
    Central PA
    Posts
    120

    function parameters and addEventListner for simple button

    I am going to have about 73 simple buttons on a couple of screens of my flash module and I'd like to obviously be able to use the same function to run all of them, but I keep getting errors. Here is the code I'm using:

    Code:
    btn_1.addEventListener(MouseEvent.MOUSE_DOWN,goLink(1));
    btn_2.addEventListener(MouseEvent.MOUSE_DOWN,goLink(2));
    btn_3.addEventListener(MouseEvent.MOUSE_DOWN,goLink(3));
    
    function goLink(num:Number) {
    counter = num;
    gotoAndStop(2);
    }
    I am trying to pass the number to the function so it will update the counter and load the right stuff on frame 2. Obviously this isn't right. I don't think I can pass a parameter via an eventListener. But how do I go about this?
    ?:-{>

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Try a dictionary:

    PHP Code:
    var reverseLookup:Dictionary = new Dictionary();
    var 
    buttons:Array = [];

    for(var 
    i:int 173i++){
        var 
    b:DisplayObject getChildByName("btn_" i);
        
    b.addEventListener(MouseEvent.MOUSE_DOWNgoLink);
        
    buttons[i] = b;
        
    reverseLookup[b] = i;
    }


    function 
    goLink(e:MouseEvent):void{
        
    counter reverseLookup[e.target];
        
    gotoAndStop(2);


  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    First, let me explain what your code is actually doing. By calling goLink(1) inside addEventListener, you're actually passing the result of goLink(1) to addEventListener. The result is void since there is no return. Clearly, that does not work as an event listener.

    I while back I wrote a utility class for creating event listeners which appear to take extra parameters (among other things). Here's a link: http://cosmodro.me/blog/2008/aug/26/...ity-functions/

    The basic idea is that you can create a function which returns another function. You can then use this technique to dynamically build your event listeners. You don't need my class to do it, but it does make things easier for you if you're not familiar with closures.

    Edit: Dictionary works just fine too.

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Location
    Central PA
    Posts
    120
    I just got this to work using substrings and the name of the button:

    Code:
    btn_1.addEventListener(MouseEvent.MOUSE_DOWN,goLink);
    btn_2.addEventListener(MouseEvent.MOUSE_DOWN,goLink);
    btn_3.addEventListener(MouseEvent.MOUSE_DOWN,goLink);
    
    function goLink(e:MouseEvent){
    var b:SimpleButton = SimpleButton(e.currentTarget);
    counter = Number(b.name.slice(4,6));
    trace(counter);
    gotoAndStop(2);
    }
    Interesting solution, though, neznein9. I will hold onto that code. Thanks.
    ?:-{>

  5. #5
    Senior Member
    Join Date
    Aug 2000
    Location
    Central PA
    Posts
    120
    Very cool, 5TonsOfFlax. I downloaded the .zip file. Thanks!
    ?:-{>

  6. #6

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