How to Limit a Number Between 2 Values

Limiting or constraining a number to a specified range of values isn’t the most common task you’ll have to perform in Flash, but it does pop up from time to time. If you encounter such a situation here is a little bit of code for your consideration:

//
// Basic concept, easy to extend.
function limitBasic (num, lo, hi){
    if (num<lo){
        return lo;
    }else if(num>hi){
        return hi;
    }else{
        return num;
    }
}
//
// Same concept, easy to remember and write.
function limitShort (num, lo, hi){
    if (num<lo) return lo;
    if (num>hi) return hi;
    return num;
}
//
// Same concept, short but hard to read.
function limitTiny (num, lo, hi){
    return num<lo ? lo : num>hi ? hi : num;
}
//
// Uses the Math object, short but a little counterintuitive and slow.
function limitSlow (num, lo, hi){
    return Math.min(hi, Math.max(lo, num));
}
//
//

All four functions work the same basic way.

  • If the number (“num”) is below the lower limit (“lo”), it is set to the lower limit.
  • If the number is above the upper limit (“hi”) it is set to the upper limit.
  • If the number is between or within both limits, it remains unchanged.

The main thing to note is that the “limitSlow” function uses the Math object which makes it a good bit slower than the other limiting methods. The other three “limit” or “constrain” functions are fundamentally the same in performance and concept but the structure of the conditional statements has been changed to suit different coding styles. If you’re not sure which one is best, use “limitBasic” as it is the most common format for “if else” conditional statements and therefore easier for most people to read.

Here’s a code sample to get you started:

//
// Limit a number between two values.
function limit (num, lo, hi){
    if (num<lo){
        return lo;
    }else if(num>hi){
        return hi;
    }else{
        return num;
    }
}
//
//
//
// Build Description textfields.
createTextField("LoDescTF", 10, 50, 10, 100, 20);
LoDescTF.text = "Lower Limit";
//
createTextField("LimitedDescTF", 20, 200, 10, 100, 20);
LimitedDescTF.text = "Limited Value";
//
createTextField("HiDescTF", 30, 350, 10, 100, 20);
HiDescTF.text = "Upper Limit";
//
//
//
// Build User-input textfields.
createTextField("LoTF", 11, 50, 30, 100, 20);
LoTF.type = "input";
LoTF.border = true;
LoTF.restrict = "0-9";
LoTF.text = "0";
//
createTextField("LimitedTF", 21, 200, 30, 100, 20);
LimitedTF.type = "input";
LimitedTF.border = true;
LimitedTF.restrict = "0-9";
LimitedTF.text = "50";
//
createTextField("HiTF", 31, 350, 30, 100, 20);
HiTF.type = "input";
HiTF.border = true;
HiTF.restrict = "0-9";
HiTF.text = "100";
//
//
//
// Build Instructions textfield.
createTextField("Instructions", 40, 10, 60, 480, 40);
Instructions.wordWrap = true;
Instructions.multiline = true;
Instructions.text = "Type a number in the middle box and press the enter key to limit the number between the low and high values.";
//
//
//
// Limit the value in the middle box when the Enter key is pressed.
LimitedTF.onKeyDown = function(){
    if(Key.isDown(Key.ENTER)){
        var lo = LoTF.text - 0;
        var hi = HiTF.text - 0;
        var lim = this.text - 0;
        this.text = limit(lim, lo, hi);
    }
}
Key.addListener(LimitedTF);
//
//

The above code produces the following SWF:

One Response to “How to Limit a Number Between 2 Values”

  1. Ralph says:

    Here is a shortcut for what I call normalizing a value. Say you have a loop that increments a counter variable and you only want the counter value to loop back around similar to your hi/low example. A neat trick is to use the modulus operator so if you want to use just degrees for a circle for example you would just do: (ctr % 360) + 1

    Or if you only want 1 – 100 to show just do (ctr % 100) + 1 and this will guarantee that you only have to deal with numbers 1 through 100.

Leave a Reply

PixelWit.com's Comment Guidelines


Warning: Undefined variable $user_ID in /home2/pixelwit/public_html/blog/wp-content/themes/fvariant2/comments.php on line 57

You must be logged in to post a comment.

© Sean O'Shell 2007-2024