AS3: check number if...
- Started
- Last post
- 5 Responses
- noiseisthis
it's 19, 39, 59, 79, etc...
if(num == 19 || num == 39 || num == 59 || ...){}
but in a condensed form
- SteveJobs0
just pop the numbers in an array, then loop through the lenght of the array returning 1 if the condition is met, and put a return 0 after the loop as a catch all
- fyoucher10
var isMatch_bool:Boolean = false;
var num:int = 69;
// The number to compare withvar compareNums_array:Array = [19, 39, 59, 79];
// Numbers you're checking//Reusable function
function isMatch (arr:Array):Boolean {
for (var i:int = 0; i < num_array.length; i++ ) {
if ( num == compareNums_array[i] ) {
trace ("We gotta match.");
return true;
}
}
}// Checking shit out
isMatch (compareNums_array) ; // FalseThere's probably a better way to do this but I've been drinking too much.
- noiseisthis0
thanks guys. i was checking for the wrong number hahaa. i needed to check for something in multiples of 20 so i did this:
if (num % 20 == 0){
trace("PARRTY");
}and it works nice :)
- ornj0
modulo is a life saver.
- rounce0
Oooor even easier:
var checkInt:int = 59;
var matchSet:Array = [19, 39, 59, 79];if(matchSet.indexOf(checkInt) != -1)
{
trace(matchSet[matchSet.indexOf... // We have a match!
trace("LEMON PARRTY!!"); // For comedic debugging purposes only.
}