PDA

View Full Version : Is the number within a range?



jdulberg
March 2nd, 2010, 10:39 AM
Hello,

I have a slider where I check the current value to see if it's within various number ranges. I'm basically running the same comparison over and over for 14 range sets. Is there a way to not have to hard code each one?

The snippet below is hard coded 14 times with incrementing boxes[x] values. boxes[x] is a predefined array:


if (sliderval >= boxes[1].position && sliderval < boxes[2].position) {
//do 1st code
}
else if (sliderval >= boxes[2].position && sliderval < boxes[3].position) {
//do 2nd code
}
//etc...


These ranges are checked as the user moves the slider button.

Any suggestions would be great!

Thanks,
Jason

Eric Drutel
March 2nd, 2010, 11:01 AM
Do a class Matcher like



class Matcher() {
private var min:int;
private var max:int;

public function matches(int sliderval):Boolean {
//if is in range return true
}

public function action(){}

}



Extend this class to override action() function;
Properly initialize min and max (do getters and setters).
Do an array which contains derived classes.
Iterate over it and
if (array[i].matches(sliderval))
array[i].action()

Shaedo
March 2nd, 2010, 02:11 PM
Just as a suggestion you might be able to do something like this (although chances are probably 50:50 that it could be applied) depending on what you //do 1st code etc is like.


for(var i:int=2; i<boxes.length; i++)//starting at i=2 as you dont seem to have a boxes[0]
{
if(sliderval<boxes[i].position)
{
//do 'i'th code
break;//stops for loop before executing more than one "//do 'i'th code "
}
}

the "//do 'i'th code " might be to run a function with 'i' as the parameter or you might just switch 'i' or you might have an array of functions that you call using 'i'.