2010-01-15 82 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