秦偉
摘要:在編寫Actionnscrip3.0腳本中,經常涉及到間隔循環執行某些任務的情況,通??梢允褂肨imer對象、eventFrame事件、setInterval()函數以及setTimeout()函數4種形式實現循環執行任務的功能。文章通過分析四種形式的運行機制及異同點,實例解析其功能為使用和選擇各種方法來為編程實踐提供參考。
關鍵詞:Actionnscrip;循環;定時
中圖分類號:TP311 文獻標識碼:A 文章編號:1009-3044(2014)18-4305-02
Strategy of Selecting Four Cycle Task Execution Method in AS3
QIN Wei
(College of Education Science, Tongren University, Tongren 554300, China)
Abstract: When the Actionnscrip3.0 script is programmed, we often face conditions of interval loop to perform certain tasks, you can often use 4 kinds form of the Timer object, eventFrame events, setInterval () function and setTimeout () function to achieve function of circular task execution. Through four forms of the analysis of the operation mechanism and the differences and similarities, example analysis for its function provide reference for programming practice as the selection and use of various methods.
Key words: Actionnscrip; Loop; Time
1 概述
Actionnscrip3.0(簡稱As3)是一種強大的面向對象編程語言,設計 As3 的意圖是創建一種適合快速地構建效果豐富的互聯網應用程序語言,這種語言以支持類型安全、代碼維護輕松、編寫簡單、響應高效等優點已成為 Web 體驗的重要部分[1]。在使用Flash軟件進行設計與制作過程中經常涉及到使用As3腳本語言來實現間隔循環執行某一任務的代碼設計,由于Flash軟件本身的特性,我們習慣采用的循環語句while,for等語句來實現這些需求是不太容易的,所以了解Timer對象、eventFrame事件、setInterval()函數以及setTimeout()函數四種形式的原理機制及特性對實現循環執行任務的功能是非常重要的。
2 運行原理解析
2.1 Timer對象
通過構造Timer對象的實例的同時設置好delay,repeatCount兩個參數來設置延遲時間和重復次數,delay的設置以毫秒計,設置好這兩個參數后就會按照設置的時間延遲定時循環執行已編好的代碼。如:
var timer:Timer = new 函數名(時間間隔,重復次數);
timer.addEventListener(TimerEvent.TIMER, 函數名);
function函數名(event:TimerEvent):void{函數體}
調用start()方法啟動timer實例:timer.start();
2.2 eventFrame事件
Event對象中的ENTER_FRAME公共常量實現該功能主要是通過幀頻來實現。每當Flash運行器執行一次預定屏幕更新檢查的時候它調度Event.ENTER_FRAME事件。注冊以接收Event.ENTER_FRAME通知的任何函數都被反復執行。……