开发者

Is there a way to check if an integer is going higher or lower in AS3

I'm trying to开发者_StackOverflow make a script that listens to a variable (int or Number) and then does certain functions whether the variable is going higher or lower. So for example if the number gets higher, it runs one function. If it gets lower, it runs another.

Is this possible in AS3? Any ideas?


Use a private variable with a setter. In the setter compare the previous value with the current value:

private var _num:Number = 0;

public function set num(value:Number) : void {
  if (value < _num) {
    //do something if it's lower
  } else if (value > _num) {
    //do something if it's higher
  } else {
    // do something if it's equal
  }
  _num = value;
}


Store the int, then set an event to be fired at the desired rate. On the event listener callback, check if the current int is higher or lower than the stored int, process what you need to and update the stored int.
Probably not a best-practice though.


If you don't have control over the source such that you can modify the setter, you can use ChangeWatcher.watch().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜