views:

275

answers:

2

Hi, I'm trying to play a movie clip when I mouse_over it. I can do it fine by doing:

mc1.addEventListener(MouseEvent.MOUSE_OVER,mover);

function mover(e:MouseEvent):void {
   mc1.play();
}

But, I want to use the same function for other movie clips, for example, to play movieclip2, movieclip3 etc.

How would I achieve this?

+3  A: 
mc1.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc2.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc3.addEventListener(MouseEvent.MOUSE_OVER,mover);

function mover(e:MouseEvent):void {
   e.currentTarget.play();
}
antpaw
+1  A: 

You can make a class to encapsulate your logic for example, to access the MovieClip from the calling function, use the property of the Event object

import flash.display.MovieClip;
import flash.events.MouseEvent;

public class PlayMovieClip {

 // register the mouse over event with whatever MovieClip you want
 public static function register(mc:MovieClip):void{
  mc.addEventListener(MouseEvent.MOUSE_OVER,mover);
 }
 // unregister the event when you dont need it anymore
 public static function unregister(mc:MovieClip):void{
  mc.removeEventListener(MouseEvent.MOUSE_OVER, mover);
 }
 // the MouseEvent will be throw whenever the mouse pass over the registered MovieClip
 // and in the MouseEvent property you have the targeted object
 // so use it
 public static function mover(e:MouseEvent):void{
   // check if we have really a MovieClip
   var mc:MovieClip=e.currentTarget as MovieClip; 
   if (mc!==null) {
     // we have a MovieClip so we can run the function play on it
     mc.play();
   }
 }
}

usage:

PlayMovieClip.register(mc1);
...
PlayMovieClip.register(mcX);

and to remove the event:

PlayMovieClip.unregister(mc1);
...
PlayMovieClip.unregister(mcX);
Patrick
this answer is correct but is a bit overkill
Allan
@Allan It depends if you want to make reusable code ;)
Patrick