参照Java的Thread类,写一个Flash的Thread类,这个类调用起来多了一两行代码,但是比较好控制和管理。和Java中Thread类非常相似。
- class com.klstudio.util.Thread extends Object{
- private var __mar:Number;
- private var __sleep_time:Number;
- function Thread(sleepTime:Number){
- if(sleepTime == undefined){
- this.__sleep_time = 50;
- }else{
- this.__sleep_time = sleepTime;
- }
- }
- private function doRun():Void{
- this.run();
- }
- public function run():Void{
- }
- public function start():Void{
- this.__mar = setInterval(this,"doRun",this.__sleep_time);
- }
- public function stop():Void{
- clearInterval(this.__mar);
- }
- public function setSleepTime(sleepTime:Number):Void{
- this.__sleep_time = sleepTime;
- }
- public function getSleepTime():Number{
- return this.__sleep_time;
- }
- }
testThread.as代码
- import com.util.Thread;
- class testThread extends Thread{
- private var __label_txt:TextField;
- function testThread(){
- super(1000);
- this.init();
- }
- private function init():Void{
- _root.createTextField("label_txt", 4, 0, 0, 100, 20);
- this.__label_txt = _root["label_txt"];
- this.initLabel();
- this.start();
- }
- private function initLabel():Void{
- this.__label_txt.autoSize = "left";
- this.__label_txt.html = false;
- this.__label_txt.textColor = 0x000000;
- this.__label_txt.wordWrap = false;
- this.__label_txt.type = "dynamic";
- this.__label_txt.selectable = false;
- }
- private function run():Void{
- var today:Date = new Date();
- var lbl:String = (today.getHours() < 10 ? "0" + today.getHours() : today.getHours()) + ":" + (today.getMinutes() < 10 ? "0" + today.getMinutes() : today.getMinutes()) + ":" + (today.getSeconds() < 10 ? "0" + today.getSeconds() : today.getSeconds());
- this.__label_txt.text = lbl;
- }
- }
Flash调用代码片段:
- stop();
- var tt:testThread = new testThread();