Skip to main content
Inspiring
June 29, 2007
Question

Functions in a class

  • June 29, 2007
  • 5 replies
  • 386 views
Ok, I have found defining classes in .as files can be very useful for me. I have a few functions that are being called over and over again in different movies. There is what I want / need to do in order to stream line the creating and updating of the flash site. I want to see if I can put those same functions into a class (.as file) so I can access them from anywhere in the flash movie. The problem is when I just try to copy and paste the functions to the .as file (func.as) and then call the function by, func.FunctionName I get errors. The movie is calling the functions when buttons are pressed. Any ideas? I will attach some functions I am using that I would like to move to the func.as file.
This topic has been closed for replies.

5 replies

Inspiring
June 29, 2007
Ok, I have updated the .as file and the .fla file. Now I am recieving the following Output error (it compiles fine).

TypeError: Error #1034: Type Coercion failed: cannot convert func@5117efe1 to flash.display.MovieClip.
at func/BackToMenu()
Inspiring
June 29, 2007
Do I need the "com.mysite.common" and will that mess up the file name? Doesn't the file name have to be that?

I already have one .as file that is loading global varibles. I would like to combine these to files into one once I get the functions working. Attached is the code for the other .as file (glo.as). I don't have to have a "package name" on that file. I just call the variables with: glo.ConnectionSpeed and it works perfectly.
Inspiring
June 29, 2007
change what you said...Still getting the same problem (error).
The New Code is attached.
Inspiring
June 29, 2007
No no....

Inspiring
June 29, 2007
OK........ Couple of things I see....

func.as

package{
public class func{ // This is the name of your class and as such, it needs a constructor.....

public function func() {
// Doesn't need to do anything
}

Your import should be inside your package but outside the class definition

You need to import your package into your Flash shell before you can call the BackToMenu funciton (this is the real reason for the error).

If you get an error about not being able to nest packages, you'll need to adjust things

package com.mysite.common {
the above line will assume that you have folders relative to your flash shell of the same names.....

com \ mysite \ common \ func.as

If this doesn't make sense, just let me know. We'll get it straightened out.
Inspiring
June 29, 2007
I have tried this. But get an error of

1119: Access of possibly undefined property BackToMenu through a reference with static type Class.