辣鸡

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

参照Java的Thread类,写一个Flash的Thread类,这个类调用起来多了一两行代码,但是比较好控制和管理。和Java中Thread类非常相似。

  1.  
  2. class com.klstudio.util.Thread extends Object{ 
  3. private var __mar:Number; 
  4. private var __sleep_time:Number; 
  5. function Thread(sleepTime:Number){ 
  6.   if(sleepTime == undefined){ 
  7.    this.__sleep_time = 50; 
  8.   }else{ 
  9.    this.__sleep_time = sleepTime; 
  10.   } 
  11. private function doRun():Void{ 
  12.   this.run(); 
  13. public function run():Void{   
  14. public function start():Void{ 
  15.   this.__mar = setInterval(this,"doRun",this.__sleep_time); 
  16. public function stop():Void{ 
  17.   clearInterval(this.__mar); 
  18. public function setSleepTime(sleepTime:Number):Void{ 
  19.   this.__sleep_time = sleepTime; 
  20. public function getSleepTime():Number{ 
  21.   return this.__sleep_time; 
  22. }
复制代码

testThread.as代码

  1. import com.util.Thread; 
  2. class testThread extends Thread{ 
  3. private var __label_txt:TextField; 
  4. function testThread(){ 
  5.   super(1000); 
  6.   this.init(); 
  7. private function init():Void{ 
  8.   _root.createTextField("label_txt", 4, 0, 0, 100, 20);   
  9.   this.__label_txt = _root["label_txt"]; 
  10.   this.initLabel(); 
  11.   this.start(); 
  12. private function initLabel():Void{ 
  13.   this.__label_txt.autoSize = "left"; 
  14.   this.__label_txt.html = false; 
  15.   this.__label_txt.textColor = 0x000000; 
  16.   this.__label_txt.wordWrap = false; 
  17.   this.__label_txt.type = "dynamic"; 
  18.   this.__label_txt.selectable = false; 
  19. private function run():Void{ 
  20.   var today:Date = new Date(); 
  21.   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()); 
  22.   this.__label_txt.text = lbl; 
  23. }
复制代码

Flash调用代码片段:

  1. stop(); 
  2. var tt:testThread = new testThread();
复制代码
posted on 2011-12-23 17:19  辣鸡  阅读(218)  评论(0)    收藏  举报