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.
Related
My code works, except it is requiring and extra "Space" at the end to be placed in order for the button to activate? Any ideas? I obviously don't have the space at the end of the user names or passwords in the code. This happens on another frame as well where I have the user type in a web address, I have the conditional set as == "md.website.com" but it is requiring "md.website.com " (extra space at the end) in order for the button to activate.
This code is expecting "AB1234 " and "newuser " instead of "AB1234" "newuser" like I need and I am telling it... I'm sorry, I'm new to AS3 and learning ALL I can, this site rocks for all the help I've already gotten!
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text == "AB1234" && password_txt.text == "newuser" )
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}
The problem is that the TextEvent.TEXT_INPUT fires before the text field is actually updated. Try using Event.CHANGE instead (or using two TextEvent.TEXT_INPUT callbacks and appending the input character with event.text within each).
I don't know why AS3 is requiring the extra space, but I removed the exact conditional, and just did a minimum character count. Of course the trainee can then type in anything as long as it matches the minimum requirement, but again, the actual usernames and passwords don't matter, its all simulation anyways, here is the code with the character count....
username_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
password_txt.addEventListener(TextEvent.TEXT_INPUT,paramChanged3);
next_btn.enabled = false;
next_btn.alpha = .5;
function paramChanged3(event:TextEvent):void
{
if (username_txt.text != "" && username_txt.length >=5 &&
password_txt.text != "" && password_txt.length >=6)
{
trace("go")
next_btn2.enabled = true;
next_btn2.alpha = 1;
next_btn2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlay_20)
}
else
{
next_btn2.enabled = false;
next_btn2.alpha = .5;
}
}
function fl_ClickToGoToAndPlay_20(event:MouseEvent):void
{
gotoAndPlay(20);
}
I have an array of Nodes 'flags', and I want to set my object's position at the first object in that array, it works and the object actually gets positioned as intended, but when I make the comparison it fails and logs 'NO'.
The line of code that sets the position works, but the comparison fails, what's wrong here?!
start: function () {
this.node.position = this.flags[0].position;
this.movement();
},
movement: function() {
if (this.node.position == this.flags[0].position) { // Problem
console.log("YES");
}
else {
console.log("No");
Update:
When I do it like this it works:
if (this.node.position.x == this.flags[0].position.x) // or position.y
Well if you write javascript here (and it looks like you do) there're two things you should know:
You can't compare objects with == out of the box
({"a":1} == {"a":1})
Will return false (you may try it yourself in your browser.
As a workaround you could do something like:
function posCompare(p1, p2){
return p1.x === p2.x && p1.y === p2.y;
}
Then use it instead of == for positions
See how I use === instead of ==? Second thing to know is Use only ===. You can learn the difference Which equals operator (== vs ===) should be used in JavaScript comparisons? but I'd keep away from == anywhere. It's slower, it may cause strange errors here and there - just don't use it at all
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.
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.
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);
}