PDA

View Full Version : How to call function in another .as file?


dhintemeyer
01-23-2007, 09:01 PM
How do I call a public function that resides in another .as file? For instance, if I'm working in foo.as and would like to call a function in bar.as, how is it done?

dr_zeus
01-23-2007, 09:48 PM
Is the function static? If so, that's easy.

bar.myFunction(param1, param2);

Otherwise, you'd need to have an instantiated bar somewhere.

var myBar:bar = new bar();
bar.myFunction(param1, param2);

That, of course, assumes that foo and bar are classes. You've been pretty vague.

shanimal
01-24-2007, 11:04 AM
I agree - this example might also help:


/* bar.as ... ***************/

class bar{

// a static function
public static function fnStatic():String{
return "I like actionscript!";
}

// an instance function
public function fnInstance():String{
return "Hi, Im Henry";
}

// an instance function using a static function
public function fnInstanceUsesStaticFunction():String{
return this.fnInstance() + bar.fnStatic();
}
}

/* bar.as ... ***************/

/* foo.as ... ***************/
// static example
trace(bar.fnStatic());

// instance example
var myBar:bar = new bar()
trace(myBar.fnInstance());

// something a little more complicated
trace(myBar.fnInstanceUsesStaticFunction())


// Calling myBar.fnStatic() throws an error
// Calling c.fnInstance() also throws an error

/* foo.as ... ***************/


I recommend a good book :
"Actionscript 3 with Design Patterns" - Adobe Press

Holla back if it isnt the shizzy - :)