Naythan
Naythan

Reputation: 33

AS3 Accessing MovieClip when added to stage on another AS class file

I've tried searching articles on here already and sadly, didn't find anything that helped solve my problem. Sorry if there are articles with the answer I am looking for and I am sure it's a simple answer, I just can't quite get my head around it.

I've got my AS stage document and two AS Class files (MAIN and gameScreen), I am trying to access the 'currentLocation' variable from MAIN inside of the gameScreen.as file.

Everything I try seems to fail and come up with an error.

          import Main;
          trace(Main.currentLocation);

          trace(MovieClip(parent).currentLocation);

My Error that I am getting:

        TypeError: Error #1034: Type Coercion failed: cannot convert Main$ to flash.display.MovieClip.

Please let me know where I am going wrong, thanks in advance guys.

Upvotes: 2

Views: 96

Answers (3)

null
null

Reputation: 5255

Simply pass it to the constructor (class names should start with a capital letter):

public function GameScreen (currentLocation:Point)
{
    //...

This does not rely on the structure of the display hierarchy (like .parent does) that is subject to change. And it doesn't care about where that variable is defined either (like root.currentLocation, static var and .parent do).

It just says "Hello, I'm a GameScreen and I need the current location. Please give it to me."

Upvotes: 2

Daniil Subbotin
Daniil Subbotin

Reputation: 6708

Make the currentLocation variable in the Main class static.

public static var currentLocation:int;

So that you can access Main.currentLocation in the gameScreen class.

Upvotes: 0

Metin Ilhan
Metin Ilhan

Reputation: 120

I use this code to get variables from main swf

var p:MovieClip = this.parent.parent as MovieClip;
trace(p.variableName);

Upvotes: 0

Related Questions