is number between 2 values - actionscript-3

What's the simplest way in actionscript to find if number is between -20 and +20, and return a true/false? I can see there's a number validator but I see it involves firing and catching events, which I think maybe overkill for the simple test I'm trying to do here.

Simplest way would be comparing the number with both values and logical combine the results:
return num > -20 && num < 20;
You may use >= or <= to include the values if needed.
You can make that into a nice function:
function isBetween(num:Number, lowerBound:Number, upperBound:Number):Boolean {
return num > lowerBound && num < upperBound;
}

Just write a function, conceptually like this:
protected function validatateNumbers(value:Number):Boolean{
if((value > -20) && (value <20)){
return true;
}
return false;
}
Then call the function whenever you want to validate your input.

Related

summing the numbers after appendText to a textField

as3 using appendText. I'm simply stringing out numbers like entering from a button press.
key2.addEventListener(MouseEvent.MOUSE_DOWN, thisButkey2);
function thisButkey2 (e:MouseEvent):void{
displayNums.appendText("2") ;
}
key3.addEventListener(MouseEvent.MOUSE_DOWN, thisButkey3);
function thisButkey3 (e:MouseEvent):void{
displayNums.appendText("3") ;
}
How do I total the string text into one number? I'd like to find out if it is > 100.
I solved it - It simply needed to be turned into a Number.
keyEnter.addEventListener(MouseEvent.MOUSE_DOWN, thisButEnter);
function thisButEnter (e:MouseEvent):void{
totalSum = Number(displayNums.text)
if ( totalSum > 100){
clearNums();
}
}
Here's a way to do this:
stage.addEventListener(KeyboardEvent.KEY_DOWN,processentry)
var entry:int;
var sum:int;
function processentry(e)
{
trace ("hello "+e.keyCode)
entry = e.keyCode - 48;
sum += entry;
trace(sum);
}
The first 'trace' simply makes clear what's happening when you press a key. We subtract 48 from the keyCode because the ASCII keyCode for '0' is '48' and the other digits' keyCodes go up sequentially. It should be easy for you to adapt this to your own case. Just let your textfield append the current 'entry'. You can put the 'sum' into another textfield or do something else with it.

ActionScript "?:" conditional operator and return in function

I have function :
public static function validate(value:*):Boolean
{
...
if(field_counter < FIELD_LIMIT){
field_counter++;
}else{
return false;
}
return true;
}
I want to make it one line, but it shown Syntax error on "return false":
field_counter < FIELD_LIMIT ? field_counter++ : return false;
If field_counter is not a negative number, you can forget that if and compute everything in a single instruction:
public static function validate(value:*):Boolean
{
return (field_counter < FIELD_LIMIT && ++field_counter)
}
The instruction ++field_counter will not be executed if field_counter is not lower than FIELD_LIMIT.
Edit
Here's a preview:
http://wonderfl.net/c/c7lA
Why make it hard on yourself and any other developer when you can make it simple?
if(field_counter >= FIELD_LIMIT)
return false;
field_counter++;
return true;
You can try working around this by testing something about the field_counter (not the nicest way but should work):
return (field_counter < FIELD_LIMIT ? (field_counter++!=null) : false);
You are attempting to stuff a return statement into a conditional. The trick is, the ?: operator returns a value, so you can do say x= y>z ? 1 : z-y; and return statement does not return a value in terms of an expression. You'd better leave the original if statement intact.

Checking for same values using if statement in actionscript?

I'm working on a match-3 style puzzle game using Flixel, and so I'm working on checking each row and column to see if there is a match at any given time. However, I have 6 different pieces (as of right now) that are active, and each piece is identified by an integer. Given that, I can check, for each and every single piece, by doing something like this:
public function matchingCheck():void
{
if (piecesArray[0][1] == 1 && piecesArray[1][1] == 1 && piecesArray[2][1] == 1) {
FlxG.log("Yay!");
}
}
However, this is rather unwieldy and would basically cause way too much repetition for my liking.
At the very least, I would like to be able to check if the values in these arrays are equal to one another, without having to specify which value it is. At the very best, I'd love to be able to check an entire row for three (or more) adjacent pieces, but I will settle for doing that part manually.
Thanks for your help!
EDIT: Nevermind, my edit didn't work. It was just checking if piecesArray[2][1] == 1, which makes me a sad panda.
EDIT 2: I've selected the correct answer below - it's not exactly what I used, but it definitely got me started. Thanks Apocalyptic0n3!
You could cut down on that code a little bit by using another function
private function checkValid( arrayOfItemsToCheck:Array, value:* ):Boolean {
for ( var i:Number = 0; i < arrayOfItemsToCheck.length; i++ ) {
if ( arrayOfItemsToCheck[i] != value ) {
return false;
}
}
return true;
}
Then you just do this in your if statement:
if ( checkValid( [ piecesArray[0][1], piecesArray[1][1], piecesArray[2][1] ], 1 ) ) {
FlxG.log("Yay!");
}
That does assume all items need to be equal to 1, though. It's still a lot of code, but it cuts out one set of "= 1 &&" for each check.
How about something like this which would tell you both if a match existed and what match it was:
public function checkForMatch():void{
var rows:int = piecesArray.length;
for(var i:int=0; i<rows; i++){
var match:int = checkRow(piecesArray[i]);
if(match > -1) {
FlxG.log("Yay you matched " + match);
}
}
}
private function ckeckRow(row:Array):int{
if(row[0] == row[1] == row[2]){
return row[0];
}
return -1;
}

Only follows second parameter

in the following code script for google Spreadsheets, I tried to make a program in which two pieces of information would be inputted to return a desired value that depends on BOTH values. Say, getValcharge ("OptionA", 2000) would return "76", or getValcharge ("OptionB",6000) would return 70. However, it seems to me that I keep getting returned the very last value possible: getValcharge("OptionA"/"OptionB"/"OptionC",1000) would return me "30". Even if I were to put an "OptionD" for the value, it would return "30" if the second number is under 5001.
Thus, it seems to only follow the second parameter --and thus only the second--even when closed off and is supposed to be not accessible to the first.
I am new to Script editor but do have modest Java experience (it'd work were this Java..) Could someone offer any advice/fixes? Any is appreciated. Thanks.
function getValcharge (valType, valAmount) {
var valcost =0;
if(valType="OptionA"){
if(valAmount < 5001)
{valcost = 76;}
if(valAmount > 5000 && valAmount <10001)
{valcost = 113;}
}
if(valType="OptionB"){
if(valAmount < 5001)
{valcost=43; }
if(valAmount > 5000 && valAmount <10001)
{valcost = 70;}
}
if(valType="OptionC")
{
if(valAmount < 5001)
{ valcost = 30; }
if(valAmount > 5000 && valAmount <10001)
{ valcost = 46; }
}
return valcost;
}
In Javascript you need to use a double-equals sign to test for equivalence, eg:
if(valType=="OptionA"){

pass < or > operator into function as parameter?

Inside my function there is an if() statement like this:
if(passedValue < staticValue)
But I need to be able to pass a parameter dictating whether the if expression is like above or is:
if(passedValue > staticValue)
But I cant really pass < or > operator in has a parameter, so I was wondering what is the best way to do this?
Also if the language I am using matters its ActionScript 3.0
Thanks!!
Instead of passing an operator, which is impossible in AS3, why not pass a custom comparison function?
function actualFunction(passedValue:Number, compareFunction:Function) {
/* ... */
if(compareFunction(passedValue, staticValue)) {
/* ... Do something ... */
}
/* ... */
}
Then to use it:
actualFunction(6, function(x:Number, y:Number) {
return x > y;
});
or:
actualFunction(6, function(x:Number, y:Number) {
return x < y;
});
Why not make the function take a bool as an argument and perform the comparison directly when calling the function?
ExampleFunction(arg1, (passedValue > staticValue))
I don't know Actionscript, but can't you make a variable called:
bool greaterThan = true;
and if its true, do >, if its false do < ?
You're right, you can't pass operators. You can pass a variable indicating which operator to use though.
lessThan = true;
func(passedValue, lessThan);