PDA

View Full Version : calling a function


bmerens
05-13-2004, 09:25 AM
hello

here is my prob :

on frame 1 : I define a function
exemple : function exemple(){
a=1;
}//end function

on frame 2 : line0 :exemple()
line1 : trace(a);

=> but there is no trace of a. the function has not been called

but if I add a button with on release exemple(), the function exemple has been called

is this normal ? how can I call and execute a function without calling it trough a button.


thanks for your help !

Berris
05-13-2004, 09:42 AM
Hi;
Not an expert on this but try using "_root.a" on both frames. You might want to try using "_root.exemple()" to call the function as well. "_root" can be relaced by "this" and the effect should be the same....

binkyboo
05-13-2004, 09:52 AM
You're calling the function properly using exemple();. Within the function you have

a = 1

and not

var a = 1

correct?

bmerens
05-14-2004, 02:06 AM
correct

on(enterFrame){
exemple();
}

i will try it

Berris
05-14-2004, 03:13 AM
Hi again...
Unless my knowledge is seriously flawed, when you write the line :
a = 1;
or
var a = 1;
inside the function, what you are doing is creating a local variable inside that function. The variable "a" only has scope within the function and once the function is exited, "a" ceases to exist. Two ways around this is to give "a" scope over both frames by making it a movieclip variable (using "_root.a" or [better] "this.a"). Of course you can always return "a" to the calling code by using

function exemple(){
a = 1;
return(a);
}

Of course in frame 2 you will have to do

trace(exemple());

binkyboo
05-14-2004, 08:09 AM
a = 1 within a function to my knowledge isn't creating a local variable within the function. var a = 1 would be.

Tink
05-14-2004, 08:21 AM
the 'a' variable will be created on the timeline that the function resides. it doesn't just have scope within the function, only arguments passed to a function will behave in this way.

binkyboo
05-14-2004, 08:31 AM
var is used to define a local variable in a function. var a = 1 is limited to the scope of function exemple.

function test(){
var a = 1;
_root.testing();
}
function testing(){
trace (_root.a);
}
test();

Tink
05-14-2004, 08:42 AM
so that will trace undefined???

thats pretty kewl. i never knew that!!

binkyboo
05-14-2004, 08:45 AM
I have senocular to thank for that information. :)