Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not sure why function not working 1

Status
Not open for further replies.

ksbigfoot

Programmer
Apr 15, 2002
856
CA
I am using Flash 8 and just learning ActionScript 2.0.

I have a basic call to a function that I am not sure why it is not working.
3 Questions:
1.) Am I passing the value into the function correctly.
2.) Can I pass a value into a function and not use it.
3.) How come in the code that works, I don't have to call the function by togglePower(), Instead I call it without the ()?
Here is my code:
NOT WORKING
Code:
function togglePower(myVar:Number):Void {
	remote_mc.light_mc.play();
}
remote_mc.power_btn.onRelease = togglePower(55);
I know the value passed in has nothing to do with the function.
This code below does work.
WORKING
Code:
function togglePower():Void {
	remote_mc.light_mc.play();
}
remote_mc.power_btn.onRelease = togglePower;
 
I forgot to add,
I realize if I do the following it works:
Code:
function togglePower(myVar:Number):Void {
    remote_mc.light_mc.play();
}
remote_mc.power_btn.onRelease = function() {
    togglePower(55);
};
But why can't I do this:
remote_mc.power_btn.onRelease = togglePower(55);
Thanks
 
I guess my only question now is
Why can't I call a function this way?
remote_mc.power_btn.onRelease = togglePower(55);
Thanks
ksbigfoot
 
Why can't I call a function this way?
remote_mc.power_btn.onRelease = togglePower(55);
You MUST assign a function to "onRelease" handler.

[tt]
function():Void{
togglePower(55)
}
[/tt]

Above is a function.

[tt]togglePower[/tt]

So is this.

[tt]togglePower(55)[/tt]

But this is not a function. This is a command to execute a function.

If you REALLY want to do:

[tt]remote_mc.power_btn.onRelease = togglePower(55);[/tt]

then you must change the function "togglePower" so that it returns a function.
For example:
[tt]
function togglePower(myVar:Number):Function {
remote_mc.light_mc.play();
return togglePower;
}
[/tt]
But there's no point in doing this.

Kenneth Kawamoto
 
Howdy Kenneth,

Thanks for the posting, just wanted to make sure I am understanding this right.

The onRelease event requires a function be assigned to it.
That is why
remote_mc.power_btn.onRelease = togglePower;
and
remote_mc.power_btn.onRelease = function() {
togglePower(55);
};
work.

And since, the line below:
remote_mc.power_btn.onRelease = togglePower(55);
is a call to a function, that goes against the rules of the onRelease event needing a function be assigned to it.

Thanks so much for your post :)
Star to you.
ksbigfoot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top