当我将鼠标移开时,我正在尝试播放影片剪辑。我可以做得很好:
mc1.addEventListener(MouseEvent.MOUSE_OVER,mover);
function mover(e:MouseEvent):void {
mc1.play();
}
但是,我想对其他影片剪辑使用相同的功能,例如,播放movieclip2,movieclip3等。
我将如何实现这一目标?
答案 0 :(得分: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 :(得分: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);