Calling a function in Actionscript 3 - function

I am trying to call a function using the function name and (); That is how I have done it in the past and how it is shown in examples on different sites. I get an error saying it is expecting 1 argument. Does calling a function require an argument?

Hi i have gone through your question you have said about calling function needed any argument here no needed they error says expecting 1 argument means you want to pass the event name in the function here in this example i have shown..
//creating event listener for card movieclip
card_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
//In this function im passing the event name done (e:MouseEvent) likewise you want to do in the function then error will clear..
function drag(e:MouseEvent):void
{
var mc:MovieClip = MovieClip(e.currentTarget);
trace(mc.name);
mc.startDrag();
}
Thank You ...

Related

Action Script 3: Calling String variable as function

I have a function,
function tempFeedBack():void
{
trace("called");
}
and event listener works perfect when I write function name directly like this,
thumbClip.addEventListener(MouseEvent.CLICK, tempFeedBack);
But, when I give the function name as string like this,
thumbClip.addEventListener(MouseEvent.CLICK, this["tempFeedBack"]());
It is not working! It says, TypeError: Error #1006: value is not a function.
Any ideas?
You got that error because simply this["tempFeedBack"]() is not a Function object, which is the listener parameter of the addEventListener() function, in addition, it's nothing because your tempFeedBack() function cannot return any value.
To more understand that, what you have wrote is equivalent to :
thumbClip.addEventListener(MouseEvent.CLICK, tempFeedBack());
Here you can see that you have passed the returned value of your tempFeedBack() function as the listener parameter which should be a Function object, but your tempFeedBack() can return nothing !
So, just to explain more, if you want that what you have wrote work, you should do something like this :
function tempFeedBack():Function
{
return function():void {
trace("called");
}
}
But I think that what you meant is :
// dont forget to set a MouseEvent param here
function tempFeedBack(e:MouseEvent):void
{
trace("called");
}
stage.addEventListener(MouseEvent.CLICK, this["tempFeedBack"]);
// you can also write it
stage.addEventListener(MouseEvent.CLICK, this.tempFeedBack);
Hope that can help.

as3 unload from child

I'm trying to unload a ProLoader from the child.
Code in main.swf
import fl.display.ProLoader;
var myProLoader:ProLoader=new ProLoader();
page2_mc.addEventListener(MouseEvent.CLICK, page2content);
function page2content(e:MouseEvent):void {
var myURL:URLRequest=new URLRequest("page2.swf");
myProLoader.load(myURL);
addChild(myProLoader);
}
function unloadcontent(e:MouseEvent):void {
myProLoader.unload();
}
Code in page2.swf:
return_mc.addEventListener(MouseEvent.CLICK,back);
function back(e:MouseEvent):void{
parent.parent['unloadcontent']();
}
When I run these, I get the following error:
ArgumentError: Error #1063: Argument count mismatch on _09Start_working_fla::MainTimeline/unloadcontent(). Expected 1, got 0. at page2_fla::MainTimeline/back()
I just want the mc in child.swf to unload the content of the ProLoader(back to main).
Thanks for help.
Regards,
Reidar Nygård
Your unloadcontent function expects an argument of type MouseEvent. In order to call it without having to pass one in, change it to:
function unloadcontent(e:MouseEvent = null):void

ActionScript 3: Assigning a named function to a variable of type Function

I'm working with a game loop and am attempting to handle user input by assigning various methods to a variable of type Function depending on the games state.
I'm assigning a defined function called InputState1 to my Function variable inputFunction:
var inputFunction:Function = InputState1;
where InputState1 is:
public function InputState1():void
{
// input logic
}
and call inputFunction in the game loop's update method:
override protected function update(timeDelta:Number):void
{
trace(inputFunction);
inputFunction();
}
When I trace the inputFunction, it returns an empty function.
Is it possible to assign a named function to a function variable and if so, how would I go about doing this properly?
Solution: I was invoking my function variables call method instead of just calling the function. It works now.
What I have understood from your argument is that you want a game state to be identified and act according to it, well you always can define a String holding the game state value which updates as game state changes, and make a function returning that string,(or directly comapre that string wth defined states)
var gameState:String="initial";
override protected function update(timeDelta:Number,gameState:String):void
{
gameState="newstate"; //depending upon your condition requirement
}
if(gameState="requredState"){
//do required stuff
}
The issue was that I was invoking my function variable's call method instead of just calling the function. It works now and I've edited the code in my question to the correct version.

how to pass this (MovieClip) as a parameter?

How can I pass the keyword this OR an instance name as a parameter inside a function?
function (reference:InstanceName):void // kind of what I want
{
reference.gotoAndPlay("frameLabel");
}
To clarify jozzeh's correct answer: your problem is variable scope. The "this" keyword's scope is contained to the owning object - you would need to establish the proper scope of the parent timeline in your function call:
function goTo( reference:MovieClip ):void
{
reference.gotoAndPlay("Start");
}
goTo(this.root); // variable scope of "this" is now at the class level
Obviously, we sometimes need parameter initializers, but in this case - a reference to 'this' is going to throw an error. If this is a function that has a changing value, sometimes the focus of which is of its own root, you'd need to handle the initializing logic outside of the method sig.
good luck!
I have never seen this being referenced like that...
The 'this' within a function is a reference to the object that initialized the function.
example:
var test:String = "testing the test."
test.myfunction();
function myfunction():void{
//this = the variable "test"
this.indexOf('test');
}
Also if you want to pass a variabel to a function it should be like this:
var test:String = "testing the test."
myfunction(test);
function myfunction(mystring:String):void{
var indexer:Number = mystring.indexOf('test');
}

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