Action Script 3: Calling String variable as function - actionscript-3

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.

Related

Calling a function in Actionscript 3

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 ...

AS3: Clicking a button via actionscript

This is my very simple code:
g2.addEventListener(MouseEvent.CLICK, buttG2);
function buttG2(event:MouseEvent):void
{
buttonNote="G2";
addWholeNote();
}
It works great when I click the button, but is it possible to fire this function from another function using Actionscript?
In some other function:
function otherFunction() {
buttG2(null);
}
You pass in null since it is never used. You could also give the parameter a default value like this:
function buttG2(event:MouseEvent = null):void
{
buttonNote="G2";
addWholeNote();
}
And then call the function without any parameters as event will then be null as per default:
function otherFunction() {
buttG2();
}
Use a default parameter of null to allow the function call from elsewhere in your code. You won't be able to get any mouseevent data, but that's probably not an issue. You could pass null into your function as you have it, but I find this to be cleaner.
g2.addEventListener(MouseEvent.CLICK, buttG2);
function buttG2(event:MouseEvent = null):void
{
buttonNote="G2";
addWholeNote();
}
call it like any other function, the param is now optional.
buttG2();

ActionScript 3 isn't supposed to be a simple synchronous architecture language?

A simple piece of code that should trace :
rien
test
done!
and I get something completely far away from that,
scenario A :
var __functions_to_execute:Array;
function start():void {
__functions_to_execute =[];
__functions_to_execute.push(futile_trace());
__functions_to_execute.push(futile_trace('test'));
execute_functions();
}
function execute_functions():void {
if(__functions_to_execute.length){
//where shift on this Array remove the first element and returns it
var exec:Function =__functions_to_execute.shift();
exec;
//I tried this too, just in case
//__functions_to_execute[0];
//__functions_to_execute.shift();
} else trace("done!");
}
function futile_trace(_value:String ='rien'):void {
trace(_value);
execute_functions();
}
start();
pretty simple. but the result is :
rien
done!
test
lets add a deprecated function to this and lets change the futile_trace function to :
function futile_trace(_value:String ='rien'):void {
trace(_value);
setTimeout(execute_functions, 0);
}
and then the result is :
rien
test
done!
Ok then, I said to myself, why not, lets change the scope when I call execute_functions, so I tried :
function futile_trace(_value:String ='rien'):void {
trace(_value);
extra_step();
}
function extra_step():void {
execute_functions();
}
guess what was the result?! yeah :
rien
done!
test
so?! Is the trace function that bad? that slow? is it the fact that passing an argument to the function take so much time compare to the other one? I mean... wow!
is there something I can do to avoid this type of weirdness ?
(For the record, my project is not to trace {rien, done and test}... I have 15k lines of codes that react completely differently if I compile them with "Omit trace statements" or not.
Thanks for your input guys.
You are executing the functions and adding their return values to the __functions_to_execute array, not the functions themselves.
Your function execute_functions doesn't actually do anything. I've tried to explain the sequence in-line:
function start():void {
__functions_to_execute =[];
// 1. traces 'rien' first because futile_trace() is called with no args
// 2. 'done!' will be traced inside execute_functions because the array is still empty
// 3.undefined will be pushed into the array next
__functions_to_execute.push(futile_trace());
// 4. traces 'test'
// execute_functions does not trace anything because __functions_to_execute is non-empty
// but it also doesn't do anything because it is just removing the `undefined` value from the start of the array.
__functions_to_execute.push(futile_trace('test'));
execute_functions();
}
Something more like this should behave how you expect. It's storing in the array function references, along with the arguments that should be passed when the function is called.
var __functions_to_execute:Array;
function start():void {
__functions_to_execute = [];
__functions_to_execute.push({func:futile_trace, args:[]});
__functions_to_execute.push({func:futile_trace, args:['test']});
execute_functions();
}
function execute_functions():void {
if(__functions_to_execute.length){
var obj:Object = __functions_to_execute.shift();
obj.func.apply(null, obj.args);
} else trace("done!");
}
function futile_trace(_value:String ='rien'):void {
trace(_value);
execute_functions();
}
start();
For scenario A, you're not actually ever pushing futile_trace to the array - you're calling it (notice the () after the function name), and then pushing the result of that call to the array.
In other words:
You call futile_trace()
futile_trace traces 'rien', because you passed no value.
futile_trace calls _execute_functions
At this point, nothing has been pushed yet, so _execute_functions traces 'done!'
_execute_functions returns.
_futile_trace returns.
The result of futile_trace() (void) is pushed.
You call futile_trace('test')
futile_trace() outputs 'test'.
futile_trace calls _execute_functions
_execute_functions shifts void from the array.
_execute_functions executes void; (which does nothing)
etc. etc.
If you need to pass a function to another function or store a reference to it in a variable, make sure you're not calling it.
__functions_to_execute.push(futile_trace);
// Use an anonymous function to pass with arguments without executing:
__functions_to_execute.push(function() { futile_trace('test'); });
... and in _execute_functions do remember the parantheses:
exec();

how to match as3 function type declaration with differing calls?

private function playSound():void
{
_soundChannel = _soundObj.play();
_soundChannel.addEventListener(Event.SOUND_COMPLETE, nextTrack);
}
<s:Button width="35" label=">>" click="nextTrack();"/>
Assuming the nextSound() function looks the same as playSound, typewise... The button click works fine, but the event listener won't call the function because its sending an argument the function isn't expecting.
I can change the nextTrack function to be evt:Event, but then the button is sending not enough arguments, and I can't find anything to type it to that will work. I can make a typed function to call the un-typed nextTrack function from the event listener
public function callnextsong(evt:Event):void{
nextTrack();
}
But i feel like there's probably a less circuitous way of accomplishing it!
Use a default parameter:
public function nextTrack(evt:Event = null):void {
It can then be called with or without the caller supplying a parameter.

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