views:

54

answers:

3

Hi,

The problem I'm facing is that I'm trying to manipulate (in this particular case add eventListeners) objects (in this case some MovieClips) on the stage from a class that isn't the document class.

1120: Access of undefined property trans.

Now I know that it's probably a scope thing and I probably can't access stage objects directly from a non document class (as I'm doing in the code below) but I can't figure out how to access them properly. I've been stuck with this problem for a couple of hours already and I've read a lot of solutions to similar problems and explanations on scope related problems but I still haven't figured out a solution. I'm hoping now that someone here can help me out.

Anyway, here's the deal:

I've got 3 dynamic text fields (called "NL", "FR" and "EN") on my stage in a movieclip called "trans". I'm trying to add eventlisteners in a second class to make them do something when clicked on.

Here's my document class:

package {

  import flash.display.MovieClip;

  // Import custom classes.
  import classes.Translate;

  public class Main extends MovieClip {

    // Init Translation class on load.
    public var translate:Translate = new Translate();

    public function Main() {

    }

  }
}

And here's my custom class Translate (which is in a subfolder "classes").

package classes {

  import flash.display.MovieClip;
  import flash.events.MouseEvent;

  public class Translate extends MovieClip {

    public function Translate() {

      // Init translation eventListeners.
      trans.NL.addEventListener(MouseEvent.CLICK,
        function(event:MouseEvent):void {
          loadNl();
        }
      );

      trans.FR.addEventListener(MouseEvent.CLICK,
        function(event:MouseEvent):void {
          loadFr();
        }
      );

      trans.EN.addEventListener(MouseEvent.CLICK,
        function(event:MouseEvent):void {
          loadEn();
        }
      );
    }

    public function loadNl() {
      trace("NL");
    }

    public function loadFr() {
      trace("FR");
    }

    public function loadEn() {
      trace("EN");
    }

  }
}

Thanks in advance for taking time to help me out.

Dries

A: 

You need to set up a reference to the Stage/Document Class. You can use another class to store the reference, i.e. StageReference. In the Main constructor you set the reference to the Stage and then it can be called via a static method.

    public class StageReference
{

    private static var _stage:Stage
    private static var _root:MovieClip;

    static public var INSTANCE:StageReference;

    public function StageReference():void
    {
    }

    static public function addStage( stageObj:Stage ):void
    {
        _stage = stageObj;
    }
    static public function addRoot( rootObj:MovieClip ):void
    {
        _root = rootObj;
    }

    static public function getStage():Stage
    {
        return( _stage );
    }

    static public function getRoot():MovieClip
    {
        return( _root );
    }
}  

// Main Class
public function Main() { 
   StageReference.addStage(stage); 
   StageReference.addRoot(this) 
 }

// Translate Class
public function Translate() { 
   var stage:Stage = StageReference.getStage();
   stage.trans.NL.addEventListener.....
}
Scott
I also tried your example Scott and I also get an error message after implementing your solution: 1046: Type was not found or was not a compile-time constant: Stage.
Dries
A: 

Scott you're right but it can be more simple. This is what I do:

public class Main
{

    public static var STAGE:Stage

    public function Main()
    {
        STAGE = this.stage;
    }
}

From that point on I just use Main.STAGE from anywhere.

Ben
Hi scott, I setted up everything like in your example but I get the following error after it: 1119: Access of possibly undefined property trans through a reference with static type flash.display:Stage.Perhaps I'm forgetting something?
Dries
I'm not Scott but try this: var trans = Main.STAGE.getChildByName("trans") as MovieClip;Remember to use whichever reference to the Stage you implemented, and the appropriate type (eg Sprite, MovieClip, Bitmap etc).
Ben
Sorry for mistaken you for Scott, Ben. Thanks for your help.
Dries
A: 

Sorry I haven't responded yet to my own question.

The solution i was looking for is rather simple and leans mostly to Ben's answer.

Basically I wanted to use my Main class in other classes. I just have to send a reference to it to the new object's class constructor.

package {

  import flash.display.MovieClip;

  // Import custom classes.
  import classes.Translate;

  public class Main extends MovieClip {

    private var _instance:MovieClip;

    public function Main() {
      _instance = this;

      var trans:Translate = new Translate(_instance);
    }
  }
}

And now I can refer to my Main class and stage objects through this reference.

package classes {

  import flash.display.MovieClip;

  public class Translate {

    private var _main:MovieClip;

    public function Translate(main:MovieClip) {
      _main = main;

      // Trace the amount of stage objects.
      trace(_main.stage.numChildren); 
    }
  }
}

Thanks Ben and Scott for helping out.

Dries