2011-04-21 58 views
2

我有一個MovieClip(稱爲「聊天」),當它被點擊時播放,但當它完成播放時我需要它來navigateToURL。這是儘可能接近,但它不起作用:需要延遲navigateToURL Flash AS3

var chatTimer:Timer = new Timer(1000); 
chats.addEventListener(MouseEvent.CLICK,startTimer); 
function startTimer(event:MouseEvent):void { 

chats.gotoAndPlay(2); 
chats.addEventListener(TimerEvent.TIMER_COMPLETE,urlAction); 
function urlAction(){ 
chatTimer.stop(); 
navigateToURL(new URLRequest("http://www.stackoverflow.com")); 
} 

希望你能幫助! Steph

回答

1

看來你想調用navigateToURL一旦movieclip達到了它的時間軸中的最後一幀。如果你使用totalFrames和currentFrame屬性,這很容易,如下所示:

chats.addEventListener(MouseEvent.CLICK, begin); 

/** 
* Begins the playing of chats 
*/ 
function begin(e:MouseEvent):void 
{ 
    // only runs if the movieclip is on frame 1 
    // (can't click it multiple times) 
    if(chats.currentFrame == 1) 
    { 
    chats.gotoAndPlay(2); 
    chats.addEventListener(Event.ENTER_FRAME, _handleChats); 
    } 
} 

function _handleChats(e:Event):void 
{ 
    var m:MovieClip = MovieClip(e.target); 

    if(m.currentFrame == m.totalFrames) 
    { 
     m.stop(); 

     var req:URLRequest = new URLRequest("http://stackoverflow.com/"); 
     navigateToURL(req, "_blank"); 

     m.removeEventListener(Event.ENTER_FRAME, _handleChats); 
    } 
} 

希望這是你以後的樣子!

+0

感謝馬蒂,我收到一條錯誤消息:「1046:未找到類型或不是編譯時常量:」。關於var req:URLRequest(「http://stackoverflow.com/」);有任何想法嗎? – stephiebee 2011-04-21 10:30:18

+0

它說哪種類型沒有找到?如果您將此代碼放入外部文件(Class),則需要導入以下內容:flash.display.MovieClip,flash.net.URLRequest,flash.net.navigateToURL,flash.events.Event。 – Marty 2011-04-21 10:32:10

+0

對不起,對我來說有點晚了。錯過了關鍵部分!嘗試上面修改後的代碼:) – Marty 2011-04-21 10:38:21