Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Calling functions from another class

New Here ,
Apr 10, 2013 Apr 10, 2013

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

TOPICS
ActionScript
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guru , Apr 10, 2013 Apr 10, 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();

Translate
Community Expert ,
Apr 10, 2013 Apr 10, 2013

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

var test:Test=new Test();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 10, 2013 Apr 10, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 10, 2013 Apr 10, 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();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 11, 2013 Apr 11, 2013
LATEST

Thanks guys, working perfectly now .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines