ActionScript 控制時間軸播放是動畫互動的基石。本文系統整理 8 大核心導航指令、參數規則、大小寫敏感性,並附完整按鈕控制練習範例,建立扎實的 AS 基礎。
| 指令 | 功能 | 參數說明 |
|---|---|---|
stop() | 暫停當前播放 | 無參數 |
play() | 繼續播放(播完循環) | 無參數 |
gotoAndPlay([scene,] frame) | 跳至指定場景/幀並播放 | scene 可省略(當前場景);frame 為數字或「標籤」字串 |
gotoAndStop([scene,] frame) | 跳至指定場景/幀並停止 | 同上 |
nextFrame() | 前進一幀並停止 | 無參數 |
prevFrame() | 後退一幀並停止 | 無參數 |
nextScene() | 進入下一場景第 1 幀 | 無參數 |
prevScene() | 進入上一場景第 1 幀 | 無參數 |
Stop() ≠ stop()gotoAndPlay("scene2", 10)gotoAndStop("intro")gotoAndPlay(10)stop()// 播放按鈕
on (release) { play(); }
// 停止按鈕
on (release) { stop(); }
// 下一幀按鈕
on (release) { nextFrame(); }
// 上一幀按鈕
on (release) { prevFrame(); }
// 跳轉播放按鈕(跳至第 50 幀播放)
on (release) { gotoAndPlay(50); }
// 三角 MC 內部程式碼(滑鼠按下暫停)
onClipEvent (mouseDown) {
stop();
}
// 主選單按鈕
btn_home.onRelease = function() { gotoAndStop("home", 1); };
btn_about.onRelease = function() { gotoAndStop("about", 1); };
btn_contact.onRelease = function() { gotoAndStop("contact", 1); };
// 選項按鈕
choice_a.onRelease = function() { gotoAndPlay("ending_a"); };
choice_b.onRelease = function() { gotoAndPlay("ending_b"); };
btn_prev.onRelease = function() { prevFrame(); };
btn_next.onRelease = function() { nextFrame(); };
btn_play.onRelease = function() { play(); };
btn_pause.onRelease = function() { stop(); };
| 錯誤現象 | 原因 | 修正 |
|---|---|---|
| 指令不生效 | 大小寫錯誤(如 Stop()) | 全小寫 stop() |
| 跳轉錯誤場景 | 場景名拼寫/大小寫不符 | 複製場景面板確切名稱 |
| 標籤跳轉失敗 | 未加引號或標籤名錯誤 | gotoAndPlay("labelName") |
| MC 內部無法控制主時間軸 | 路徑錯誤 | 用 _root.gotoAndPlay(...) |
// AS3 寫法(事件監聽模式)
btn_play.addEventListener(MouseEvent.CLICK, function():void { play(); });
btn_stop.addEventListener(MouseEvent.CLICK, function():void { stop(); });
btn_next.addEventListener(MouseEvent.CLICK, function():void { nextFrame(); });
btn_prev.addEventListener(MouseEvent.CLICK, function():void { prevFrame(); });
btn_goto.addEventListener(MouseEvent.CLICK, function():void { gotoAndPlay(50); });
AS3 建議將程式碼集中在時間軸第 1 幀或文件類別,避免分散在物件上。