AS3 odd or even (mal)function - actionscript-3

please
how to fix this AS3 function ?
thank you
function dispari(numero:int):Boolean;
{
//check if the number is odd or even
if (numero % 2 == 0)
{
returns false;
}
else
{
returns true;
}
}
ERROR:
1071: Syntax error: expected a definition keyword (such as function) after attribute returns, not false.

Why do you have a semi-colon (;) at the end of your function statement? I don't do any AS3 coding but it doesn't look right, and a cursory glance at a few samples on the web don't have it there.
I suspect that may be what's causing your problem. Try this instead:
function dispari(numero:int):Boolean
{
//check if the number is odd or even
if (numero % 2 == 0)
{
return false;
}
else
{
return true;
}
}
I've also changed the return statements to match what every other piece of AS3 does to return values (thanks, #Herms, forgot to mention that :-)

Pax is correct with there answer, but you can simplify it by just returning the result:
function dispari(numero:int):Boolean
{
return (numero % 2 != 0);
}

Related

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.

regexp.test() returns boolean but not on 'if' statement - always evaluates to

I am working on a standalone interface.
I have several text boxes, and all have Names and IDs.
If I run this code:
var re = /someregexp/g;
var k ="sometext";
textBoxOne.setText(re.test(k)); //textBoxOne was selected first
The correct result 'true' or 'false' is displayed.
asking for a 'typeof(re.test(k))' correctly returns boolean.
However this code:
if (re.test(k)) {
textBoxTwo.setText("matched.");
} else {
textBoxTwo.setText("NOT matched.");
}
Always goes into the 'else' branch. if (re.test(k) == true) renders the same result.
Looks like a bug to me, anyone else also found this?
Edit: AdamL has pointed out (thanks Adam), in his comments below, that there is more to this than I supposed. I leave my original code below for reference.
The myFunction2() below follows the "matched" branch as you would expect a correctly working regex (so perhaps this can be used to work around the bug):
function myFunction2() {
var k ="sometext";
Logger.log( /ome/g.test(k) );
if ( /ome/g.test(k) ) {
Logger.log("matched.");
} else {
Logger.log("NOT matched.");
}
}
Conversely, as you have observed, the myFunction() below unexpectedly follows through to the "NOT matched" branch.
function myFunction() {
var re = /ome/g;
var k ="sometext";
Logger.log(re.test(k));
if (re.test(k)) {
Logger.log("matched.");
} else {
Logger.log("NOT matched.");
}
}

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

What does this line of Actionscript do?

I'm looking at the as3delaunay library and most of the code is clear to me. This part is not, however (note the line that I put preceded with an arrow):
public function circles():Vector.<Circle>
{
var circles:Vector.<Circle> = new Vector.<Circle>();
for each (var site:Site in _sites)
{
var radius:Number = 0;
var nearestEdge:Edge = site.nearestEdge();
=======>> !nearestEdge.isPartOfConvexHull() && (radius = nearestEdge.sitesDistance() * 0.5);
circles.push(new Circle(site.x, site.y, radius));
}
return circles;
}
For reference, isPartOfConvexHull() is found in Edge.as and looks like this:
internal function isPartOfConvexHull():Boolean
{
return (_leftVertex == null || _rightVertex == null);
}
What does !nearestEdge.isPartOfConvexHull() do? Does that mean that the radius = nearestEdge.sitesDistance() * 0.5 only executes if false is returned from the call to isPartOfConvexHull()? Does that stop execution of any other code?
It is equivalent to:
if (!nearestEdge.isPartOfConvexHull()) {
radius = nearestEdge.sitesDistance() * 0.5;
}
In the following line:
var b:Boolean = expression1 && expression2;
expression2 will not be evaluated if expression1 is false because we already know the final result: b = false.
Now in the following line:
expression1 && expression2;
The same thing happens except the fact that we are not assigning the result to a variable.
And this is exactly what happens in the line you are asking about where !nearestEdge.isPartOfConvexHull() is the first expression and (radius = nearestEdge.sitesDistance() * 0.5) is the second expression.
To extends #sch answer with some explanations (I didn't knew if editing answer to almost double it was ok).
This is based on lazy execution of the interpreter. If (!nearestEdge.isPartOfConvexHull()) is False then there's no need to execute the second part of the AND statement to know it'll be False, then it's left unexecuted. If it's true the evaluation of the complete statement is needed (and then done) to tell wether or not this boolean is True. So this is equivalent to an if statement.
TMHO this is bad code since it's to much condensed and hard to understand.

AS3 Compiler error 1083: problem with else syntax

I'm creating a simple click and scroll for a future menu for my personal site. I have a box, I called it thing_mc, and I have 3 positions for it. I have a next and prev. button that will control thing_mc position. I'm using TweenLite to animate thing_mc, if that makes a difference.
I get a 1083 error (...else is unexpected) and (...rightparen is unexpected).
Can anyone tell me why and how I can resolve this?
Thanks
import gs.TweenLite;
next_mc.addEventListener(MouseEvent.CLICK, nextListener);
prev_mc.addEventListener(MouseEvent.CLICK, prevListener);
//prev_mc.visible = false;
function nextListener(event:MouseEvent):void
{
if(thing_mc.x == 400);
{
TweenLite.to(thing_mc, .5, {x:50, y:50});
}
else if //// i get error 1083 here (...else is unexpected)
{
TweenLite.to(thing_mc, .5, {x:-100, y:50}); //// i get error 1083 here (...rightparen is unexpected)
}
}
function prevListener(event:MouseEvent):void
{
if(thing_mc.x == -100);
{
TweenLite.to(thing_mc, .5, {x:400, y:50});
}
else if //// i get error 1083 here (...else is unexpected)
{
TweenLite.to(thing_mc, .5, {x:500, y:50}); //// i get error 1083 here (...rightparen is unexpected)
}
}
next_mc.buttonMode = true;
prev_mc.buttonMode = true;
I am not AS expert, but the semicolon after if(thing_mc.x == 400); and if(thing_mc.x == -100); seems strange. Should rather read if(thing_mc.x == 400) and if(thing_mc.x == -100) I'd say.
else if //// i get error 1083 here (...else is unexpected)
You have a few options here. You can either use a second condition, if you want to use else if i.e.
else if (someCondition) { ...
or, use just else
else { ...
or use another if
if { ...
It all depends on what you want to achieve.
From what I can see, the second option (plain else) looks like what you want.
function nextListener(event:MouseEvent):void
{
if(thing_mc.x == 400);
{
Don't use ; after the if brackets
;-)
Wats going on is the parser sees
if ( cond ) ;
as
if ( cond ) /empty statement/ ; //semicolon ends empty statement
(and the else if /.../ of course lacked the parenthesized conditional expr that an if requiress).
I've seen this semicolon related error a lot.
It may be because semicolons are optional in some cases in as3.