A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Need help: using a loop to insert instance names into a function/event listener

  1. #1
    Junior Member
    Join Date
    Feb 2008
    Posts
    1

    Unhappy Need help: using a loop to insert instance names into a function/event listener

    Hello, I am having trouble with a loop. I would like to add mouse events to a few movieclips, but I don't want to copy and paste the event listener and function over and over for each individual MC. I've tried a loop with an array of the instance names (I tried using the same technique with PHP), but it doesn't seem to work for me. Could you please look at the code below and help me out? I am pretty slow with ActionScript, so apologies if my terminology is off. If you would like the .fla file, just ask.

    PHP Code:
    var colorList:Array = new Array("green","yellow","blue");

    for 
    each( var color in colorList ) {
        
        
    color.addEventListenerMouseEvent.MOUSE_OVERmouseOn );
        
    color.addEventListenerMouseEvent.MOUSE_OUTmouseOff );
        
        function 
    mouseOne:MouseEvent ):void {
            
    color.gotoAndStop(2);
        }
        
        function 
    mouseOffe:MouseEvent ):void {
            
    color.gotoAndStop(1);
        }
        

    Thank you.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Instead of putting the names in the array, put the actual instances. Also, you can't define named functions in another function like that. (Actually, you pretty much can, but not like that and it's advanced stuff, and you don't need to here).
    Code:
    var colorList:Array = new Array(green, yellow, blue);
    
    for each( var color:MovieClip in colorList){
      color.addEventListener(MouseEvent.MOUSE_OVER, mouseOn);
      color.addEventListener(MouseEvent.MOUSE_OUT, mouseOff);
    }
    
    ...
    
    function mouseOn(e:MouseEvent):void{
       (event.currentTarget as MovieClip).gotoAndStop(2);
    }
    
    function mouseOff(e:MouseEvent):void{
      (event.currentTarget as MovieClip).gotoAndStop(2);
    }

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