1084: Syntax error rightparen before function - actionscript-3

i have been learning action script 3 and now i get 2 errors i cant fix the main one being 1084: Syntax error rightparen before function
var blueS_1 = new blueS();
blueS_1.x = 50;
blueS_1.y = 50;
addChild(blueS_1);
blueS_1.addEventListener(MouseEvent.MOUSE_DOWN,onclick
function onclick(e:MouseEvent);:void {
trace("click!");
}

You should look some examples while learning. And when facing an issue, search for a solution and compare your code to examples.
You have couple very simple mistakes there.
Your code:
blueS_1.addEventListener(MouseEvent.MOUSE_DOWN,onclick
function onclick(e:MouseEvent);:void {
trace("click!");
}
Example:
blueS_1.addEventListener(MouseEvent.MOUSE_DOWN,onclick);
function onclick(e:MouseEvent):void {
trace("click!");
}
So, missing ); from event listener line and extra ; at function opener.

Related

MACROS in gamemaker function throwing error

This Code
var within_rangex = is_within(x,o_player.x,PLAYER_REACH);
throwing the error
Got ';' (;) expected ','
I Have No Idea Why, when using magic numbers there is no error thrown.
EDIT:
The Script In Which PLAYER_REACH Is Defined.
// This Script Contains Code To Create enums And macros
function enums(){
enum states{
normal,
jumping
}
#macro TILE_REFRESH_RATE 10; // How Often Will A Tile Update Accure
#macro PLAYER_REACH 64; // How Far Can The Player Reach
}
I Found It. The Simi-colons Can't Be Used In MACROS. Always Forget This.
Here Is The Fixed Code.
// This Script Contains Code To Create enums And macros
function enums(){
enum states{
normal,
jumping
}
#macro TILE_REFRESH_RATE 10; // How Often Will A Tile Update Accure
#macro PLAYER_REACH 64; // How Far Can The Player Reach
}

Flash AS3 : HitTest unexpected error

If have a Problem with this bit of code in my Flash AS3 Project:
//Hit Test between Objects
function hitTest(a,b:DisplayObject):Boolean {
if (b.hitTestObject(a) == true) then {
return true;
} else {
return false;
}
}
I am calling it like so:
trace(hitTest(player, c_lvl.gold1));
Player is an Object created in my Code (instance of a movieclip):
player = new Player();
world.addChild(player);
gold1 is an instance of a movieclip packed in another movieclip called c_lvl.
Im getting 2 errors:
/Users/nicolasbrauch/Google Drive/info_1/script.as, Line 141 1008: Attribute is invalid.
/Users/nicolasbrauch/Google Drive/info_1/script.as, Line 143 1083: Syntax error: else is unexpected.
Line 141 is the first Line of my hitTest function.
What shall I do? Where is my error? I dont get it...
As akmozo said, you shouldn't have a then keyword after your if conditional. Once you remove that keyword, you will have fixed both errors.

Im trying to add a child in as3 but it does not work

What am I doing wrong
SpawnTeams();
var Redone:RedTeam = new RedTeam();
function SpawnTeams():void{
addChild(Redone);
}
I get this error:
Type error 2007
My guess is that you are calling SpawnTeam before you have declared Redone. Switch round the lines 'var Redone:RedTeam...' with 'SpawnTeams();'
var Redone:RedTeam = new RedTeam();
SpawnTeams(); //This line is moved
function SpawnTeams():void{
addChild(Redone);
}

As3 - script work at root timeline but not inside MovieClip

Please help me inspect this code:
function btntxt(target:String, txt:String):void
{
var button:MovieClip = MovieClip(this.getChildByName(target));
** var btnText:TLFTextField = TLFTextField(button.getChildByName("btnText"));
btnText.text = txt;
button.gotoAndStop(1);
button.buttonMode = true;
button.useHandCursor = true;
button.addEventListener(MouseEvent.MOUSE_OVER,overListener);
button.addEventListener(MouseEvent.MOUSE_OUT,outListener);
button.addEventListener(MouseEvent.MOUSE_DOWN,clickListener);
button.addEventListener(MouseEvent.MOUSE_UP,upListener);
}
When I debug, it gives me error at **:
1046: Type was not found or was not a compile-time constant: TLFTextField.
1180: Call to a possibly undefined method TLFTextField.
Also output error 1065. This code works at the top level but when I copy it inside a Movieclip's timeline, it doesn't work! Why?
If this information is short, please tell me.
Add the import statement at the top of your code:
import fl.text.TLFTextField;

Passing e:MouseEvent as an argument via setInterval

So i have this function
capture_mc.buttonMode = true;
capture_mc.addEventListener(MouseEvent.CLICK,captureImage);
function captureImage(e:MouseEvent):void {
//lalalala
}
I want to call this function every 2 seconds (after mouse click event happens).
I tried using setInterval
setInterval(captureImage,2000,e:MouseEvent);
but it leads to following error
1084: Syntax error: expecting rightparen before colon.
What's wrong ?
And ya, i am new to AS.
First, since this is AS3 you should be using Timer and TimerEvent. I'll show you how in the example.
Now you'll need to separate your functions:
edit: I've updated this to be safer based on #(Juan Pablo Califano) suggestions. I would keep the same timer for ever if the amount of time isn't going to change.
// first param is milliseconds, second is repeat count (with 0 for infinite)
private var captureTimer:Timer = new Timer(2000, 0);
captureTimer.addEventListener(TimerEvent.TIMER, handleInterval);
function handleClick(event:MouseEvent):void
{
// call here if you want the first capture to happen immediately
captureImage();
// start it
captureTimer.start();
}
function handleInterval(event:TimerEvent):void
{
captureImage();
}
function captureImage():void
{
// lalalala
}
You can also stop the timer with captureTimer.stop() whenever you want.
The problem is that you should use the parameterName:ParameterType syntax only when declaring formal parameters (or when declaring vars and consts). Meaning, this is valid only when you are defining a function:
function func(paramName:Type){
}
When you call the function, you don't have to put the type of the arguments.
So, your function call should look like this:
setInterval(captureImage,2000,e);