var now:int = 0;
var thn:int = 0;
function loop():void
{
// Change the number.
now = changingNumber;
if(now > thn)
{
trace("Got larger.");
}
else if(now < thn)
{
trace("Got smaller.");
}
else
{
trace("Maintained value.");
}
// Store the changed value for comparison on new loop() call.
// Notice how we do this AFTER 'now' has changed and we've compared it.
thn = now;
}
或者,您可以为您的值准备一个 getter 和 setter,并在那里管理增加或减少。
// Properties.
var _now:int = 0;
var _thn:int = 0;
// Return the value of now.
function get now():int
{
return _now;
}
// Set a new value for now and determine if it's higher or lower.
function set now(value:int):void
{
_now = value;
// Comparison statements here as per above.
// ...
_thn = _now;
}
这种方法效率更高,不需要循环。