[AS 2.0]计数器类
很久没钻研了,这样下去不是办法
静下心来写了一个计数器类

只是——结构混乱的连自己都有点看不下去~~~
收心啊收心……
===================================================


Count 类

[说明]

用来创建一个自定义 计数区间/计数步长/计数频率 的计数器类,它将使用 num 属性来返回计数指针。


[构造]

Count 构造函数
public Count ($outset:Number, $finish:Number, $step:Number, $interval:Number)
$outset:Number 计数区间的初始数值
$finish:Number 计数区间的终止数值
$step:Number 计数器的步长
$interval:Number 计数器的计数频率;单位:秒


[属性]

num:Number
返回当前对象的计数指针;读写属性。


[方法]

start
声明:public function start (Void):Void
说明:开始运行当前计数对象

pause
声明:public function pause (Void):Void
说明:暂停运行当前计数对象

addEventListener
声明:(继承自 mx.events.EventDispatcher)
说明:注册一个对象以接收 onStart、onPause 和 onComplete 侦听器的通知

removeEventListener
声明:(继承自 mx.events.EventDispatcher)
说明:从接收事件通知消息的对象列表中删除对象。


[事件]

onStart
说明:当计数指针开始移动时调用

onPause
说明:当计数指针暂停时调用

onComplete
说明:当计数完成后调用


[举例]

import Count;
//以下构造了一个 0 到 30 的计数器,步长为5,计数频率为 0.5次/秒
var myCount:Count = new Count (0, 30, 5, 2);
//mcStart、mcStop 为主场景的按钮实例名
mcStart.onPress = function ()
{
	myCount.start ();
};
mcStop.onPress = function ()
{
	myCount.pause ();
};
var myListener:Object = new Object ();
myListener.onStart = function ()
{
	trace ("Start");
};
myListener.onComplete = function ()
{
	trace ("Complete");
};
myListener.onPause = function ()
{
	trace ("Pause");
};
myCount.addEventListener ("onStart", myListener);
myCount.addEventListener ("onComplete", myListener);
myCount.addEventListener ("onPause", myListener);



以下为 Count 类的内部实现
/**
* 名称: Count
* 描述: 计数类;自定义计数区间、自定义计数步长、自定义计数频率
* 作者: =棉花糖=
* 时间: 2005/11/7
* 版本: 1.0
*/
import mx.events.EventDispatcher;
class Count
{
	//========================================
	//property
	//========================================
	private var mNum:Number;
	private var mFinish:Number;
	private var mStep:Number;
	private var mInterval:Number;
	private var mFps:Number;
	private var mIsAdd:Boolean = true;
	private var mStartIsLook:Boolean = false;
	private var mPauseIsLook:Boolean = true;
	//========================================
	//constructor function
	//========================================
	public function Count ($outset:Number, $finish:Number,
$step:Number, $interval:Number) { EventDispatcher.initialize (this); if ($outset > $finish) { mIsAdd = false; } mNum = $outset; mFinish = $finish; mStep = $step; mInterval = $interval; } //======================================== //public method //======================================== //======================================== //start public function start (Void):Void { var _o:Object = this; if (mStartIsLook == false) { mFps = setInterval (countStart, mInterval * 1000, _o); _o.dispatchEvent ({target:this, type:"onStart"}); mStartIsLook = true; mPauseIsLook = false; } } //======================================== //pause public function pause (Void):Void { var _o:Object = this; if (mPauseIsLook == false) { clearInterval (mFps); _o.dispatchEvent ({target:this, type:"onPause"}); mStartIsLook = false; mPauseIsLook = true; } } //======================================== //get mNum public function get num ():Number { return mNum; } //======================================== //set mNum public function set num ($n:Number):Void { mNum = $n; } //======================================== //EventDispatcher public function dispatchEvent () { } public function addEventListener () { } public function removeEventListener () { } //======================================== //private method //======================================== //======================================== //countStart private function countStart ($o:Object):Void { if ($o.mIsAdd == true && $o.mNum + $o.mStep <= $o.mFinish) { $o.mNum += $o.mStep; } else if ($o.mIsAdd == false && $o.mNum - $o.mStep >= $o.mFinish) { $o.mNum -= $o.mStep; } else { clearInterval ($o.mFps); $o.dispatchEvent ({target:this, type:"onComplete"}); $o.start = undefined; $o.pause = undefined; } } }
[2005-11-8 0:42]

{CommentAuthor} [{CommentTime}]  
{CommentContent}