adamzwakk
adamzwakk

Reputation: 709

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

I'm trying to 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?

Upvotes: 2

Views: 244

Answers (3)

houser2112
houser2112

Reputation: 164

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

Upvotes: 0

Jorge Guberte
Jorge Guberte

Reputation: 11054

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.

Upvotes: 0

Robusto
Robusto

Reputation: 31883

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;
}

Upvotes: 7

Related Questions