2010-01-15 96 views
0

我试图播放影片剪辑,当我mouse_over它。我可以通过做做精:Actionscript 3 mouse_over播放电影

mc1.addEventListener(MouseEvent.MOUSE_OVER,mover); 

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

不过,我想使用其他影片剪辑相同的功能,例如,打movieclip2,movieclip3等

我将如何实现这一目标?

回答

3
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(); 
} 
1

你可以让一个类来封装你的逻辑,例如,从调用函数访问的MovieClip,使用Event对象

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(); 
    } 
} 
} 

使用的财产:

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

并删除事件:

PlayMovieClip.unregister(mc1); 
... 
PlayMovieClip.unregister(mcX); 
+0

这个答案是正确的,但有点矫枉过正 – Allan 2010-01-15 13:42:45

+0

@Allan这取决于你是否想制作可重复使用的代码;) – Patrick 2010-01-15 14:00:56