PDA

View Full Version : AS3 OOP - calling Main.function does not work



xchrisx
March 15th, 2009, 03:30 PM
This has been stumping me for days, maybe you can help. I can't seem to access functions (methods) of my document class from any other class in the same package. What i need to do is create some global functions like menu reset, title change, etc. Maybe i'm not fully understanding the OOP concept, because this seems simple and it should totally work, but it doesn't and no documentation has been able to tell me why.

I have a document class called Main.as like this:

package
{
import flash.display.Sprite;
import flash.text.TextField;

public class Main extends Sprite
{
public var myText:TextField = new TextField();

public function Main()
{
myText.text = "Hello.";
addChild(myText);
}

public function change_text() {
myText.text = "Hello there.";
}

}
}

Works great. Then I have a MovieClip on the stage with the Linkage class Item.as:

package
{
import flash.display.MovieClip;
import flash.text.TextField;

public class Item extends MovieClip
{
public function Item()
{
Main.change_text();
}
}
}

But for some reason it gives me the error:

Item.as, Line 10 : 1061: Call to a possibly undefined method change_text through a reference with static type Class.

Your help is greatly appreciated.

Krilnon
March 15th, 2009, 03:39 PM
It looks like you're trying to access the change_text method as though it were a static/class method on Main. Flash puts an instance of the document class on the stage, so you would need a reference to that instance to call instance methods on Main.

Since you linked Item to something that is on the stage, you should be able to reach the document class instance by using code like this:
Main(parent).change_text();

That could wouldn't work if you happen to have put the Item instance within another MovieClip. In that case, you'd need to add another parent to the chain so that it'd be:
Main(parent.parent).change_text();

xchrisx
March 15th, 2009, 04:28 PM
So the document class becomes an instance on the stage. Puts a lot of stuff into perspective. Thanks man, you're a freakin' genius.

Can I ask you one more related question? If I were to make change_text a static function like this:

public static function change_text() { myText.text = "Hello there."; }
then I can call it from Item.as easily, but now it loses its scope and no longer recognizes the textfield on the stage, myText. I get the error:

Main.as, Line 19: 1120: Access of undefined property myText.
And I can't use this in a static function, so i'm lost again. It seems to defeat the purpose of making a function global if it's scope spins out of orbit. So where does the static function go and how do i access my class variables?

Also, am I missing a fundamental concept of OOP, or is this just normal learning curve with AS3 displayObjects?

Krilnon
March 15th, 2009, 05:04 PM
static functions aren't supposed to be able to access instance variables. If they could, how would the function know which instance's variables to use? In other words, if you had two instances of Main, which myText would be accessed by the static functions of Main?

xchrisx
March 15th, 2009, 05:36 PM
I see, that makes a lot of sense. I'll play around with it some more and see if i can figure it out further.

Thanks again for your time, it's very helpful. I've got a stack of books here but i can't ask them questions like these.

dtotheor
April 3rd, 2009, 11:36 AM
krilnon, you have saved me much anguish. Thank you!