Copy link to clipboard
Copied
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
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();
Copy link to clipboard
Copied
if you wanted to create a Test instance from Main, you could use:
var test:Test=new Test();
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
Thanks guys, working perfectly now .
Find more inspiration, events, and resources on the new Adobe Community
Explore Now