PDA

View Full Version : Public Functions (for all MC's)



Nick23
October 6th, 2009, 09:37 PM
Hello,

I am trying to make a function such that every instance, or Movie Clip, can use the function as well.

How would I do this?

Thanks!

dandylion13
October 6th, 2009, 09:48 PM
You might be looking for infomation on "static methods". Do you now what those are and how to use them?

Nick23
October 6th, 2009, 09:52 PM
You might be looking for infomation on "static methods". Do you now what those are and how to use them?

Not completely. Can someone explain what they are?
I think I know how they are used, though. It would be something like public static function Funct() {}, right?

I know you can do public static function Funct() {} and then to call it with mc.Funct(); but is there a way to somehow import the function so all you need to type is Funct();?

snickelfritz
October 6th, 2009, 10:43 PM
I assume this is regarding an event handler (a function called by an event listener), such as when a movieclip is clicked.
Use "e.currentTarget", instead of referencing the movieclips by name.
You would then only need one event handler.

Nick23
October 10th, 2009, 12:12 PM
No this is not regarding an event handler...

dandylion13
October 10th, 2009, 12:24 PM
Yes, you're right about how static methods work, so that's cool! :)

Is there is a reason that you need to use

function();

rather than...

Class.function()

?

The reason I ask is because I can't think of a scenario where it would make any difference. But, maybe you know of one!

So, I'd love to know a little more about the context of your problem, because the solution might be different than you think it is.

IQAndreas
October 10th, 2009, 02:25 PM
So, are you looking for a global function that can be called from anywhere in your code just by giving the function name, or do you want all movie clips of a certain type to be able to call this function by only typing the function name?

Are you familiar with using external classes, or do you code entirely on the timeline?

Nick23
October 26th, 2009, 10:07 PM
are you looking for a global function that can be called from anywhere in your code just by giving the function nameYes.

Is there is a reason that you need to use function(); rather than... Class.function()Not really, I'm just being lazy and don't want to type MovieClip.function() every time.

Scythe
October 26th, 2009, 11:07 PM
Define a function in the top level of a package, then import the function (or the whole package).


package myPackage {
// This is in a file called funct.as in a folder called
// myPackage which is either in the same folder
// as the calling code or in your classpath.
public function funct() {
// code here
}
}

Shaedo
October 26th, 2009, 11:48 PM
you might want to consider 'include'
http://help.adobe.com/en_US/AS3LCR/Flash_10.0/statements.html#include
it is the same as if you had typed the code (in your case function in) at the point you include the file. but (I think) it will not take up so much file space. A note that you just type the code straight in the file your including, so no 'package' or 'class' declarations needed.

Luck,
S.