Skip to main content
Participating Frequently
April 11, 2013
Answered

Calling functions from another class

  • April 11, 2013
  • 1 reply
  • 1355 views

How does one access a function from a different class?

Let's say I have an .as file called Main. My flash file uses this as it's main class, so it runs when you test the program. Let's say I have another class called Test which looks like this :

package

{

     public class Test

     {

                         public function Test()

                         {

             trace ("why won't you work?!");

        }

    } 

}

Say I want to call the Test function when the game runs (by calling it in Main), how would I get it into the Main file to run? I've imported it, made sure the functions were public/static, created it as a variable and it still won't work.

Any help would be good

This topic has been closed for replies.
Correct answer moccamaximum

the constructor of a class is different than a "normal" function.

so you can`t do something like

var meow:Test=new Meow();

you have to call it so:

var test:Test=new Test();

test.Meow();

1 reply

kglad
Community Expert
Community Expert
April 11, 2013

if you wanted to create a Test instance from Main, you could use:

var test:Test=new Test();

Dinoman44Author
Participating Frequently
April 11, 2013

Haha that worked, no idea why it wasn't working in my other programs. What about if I wanted to access ANOTHER function from that same class :

package

{

     public class Test

     {

             public function Test()

        {

             trace ("why won't you work?!");

        }

       

              public function Meow()

        {

             trace ("meow!");

        }

    }

}

And I wanted to call the Meow function from Main. I try and it says it's undefined

moccamaximumCorrect answer
Inspiring
April 11, 2013

the constructor of a class is different than a "normal" function.

so you can`t do something like

var meow:Test=new Meow();

you have to call it so:

var test:Test=new Test();

test.Meow();