views:

14156

answers:

5

I'm trying to call a function after I load some XML into Actionscript, and I'm just wondering how I call that function. Example:

//Function Declarations
function parentFunction()
{
    function callMe()
    {
     textField.text = "lawl";
    }
}

Now, how do I call the "callMe()" function in a different part of the code, like an onRelease function?

on(release)
{
callMe(); //doesn't work
parentFunction().callMe(); //doesn't work
parentFunction.callMe(); //doesn't work
}

ActionScript 2.0 is just so wonky! Any ideas?

+2  A: 

Are you forced to declare callMe inside of parentFunction? I assume so because otherwise you would just do

function parent() { } function callMe() { }

To be clear, a function can't own another function unless you provide some scope for that function to live in.

So in JavaScript, you would do this by using the prototype object to declare the callMe function as a method of the object that parentFunction returned.

http://www.howtocreate.co.uk/tutorials/javascript/objects

For ActionScript, read this article on Adobe's website: http://www.adobe.com/devnet/flex/articles/as_collections_03.html

EDIT: After some more reading it appears the way you did things, you are actually declaring callMe as a private function. See this article which should make the whole private/public javascript issue a lot more understandable.

Michael Pryor
A: 

I'm an idiot. I forgot the whole "a function can't own another function" thing, so I figured out another way to do it. Thanks!

Matt S
A: 

Of course a function can "own" another function. this is ECMAScript remember. Just declare a variable inside your function and assign a function to it. You can then call your function by using the "call" method.

function foo() { trace("foo");

var bar = function()
{
 trace("bar");
};

bar.call();

}

foo();

A: 
//v--- on the frame
function callMe(){
textArea.text='lawl';
}
//v---- for button
on(release){
callMe();
}

--- or -----


//CUSTOM!!
//v---- on frame
function callMe(say){
textArea.text=say;
}
//v--- for button
on(release){
callMe('lawl');
}
A: 

Sorry for bad english, Set a handler for (Menu-)Buttons, whitch are located in a MC.

MC "Buttons" on Stage (with 3 "testbtn" in it) @ first frame:

function SetMethod(Method:Function){
    //trace(Method.call()); 
    //or something like:
    testbtn1.addEventListener(MouseEvent.CLICK, Method);
    testbtn2.addEventListener(MouseEvent.CLICK, Method);
    testbtn3.addEventListener(MouseEvent.CLICK, Method);
}

Stage (with MC "Butttons" in it) @ first frame:

function TheMenuListener(evt:Event):void{
    trace(evt.target.name);

}
...
Buttons.SetMethod(this.TheMenuListener); 

returns the testbtn.name

Edit: Oh, it´s for AS3, but maybe helps!

freak_ts