0

I have a movieclip and I am going to add a pan feature to it. I want to add the pan as it's own class that the movieclip can reference.

When I create the pan class, I send the movieclip to it so I have it's position properties at all times.

What is the best way to access the stage in the pan class? I will need to be able to get the stage mouseX, mouseY, stageWidth, and stageHeight.

Right now, I have the pan class extend a sprite object and actually add it as a child of the movieclip I want to pan.

Would it be better to just send the stage itself into the pan class as well or is there a better way than this?

Sean
  • 1,758
  • 3
  • 20
  • 34
  • The proper way would be to have pan class extend MovieClip then instead of making your MovieClip a MovieClip you make it an instance of Pan class. Then all the properties of the MovieClip class will be inherited. – The_asMan Jun 12 '12 at 20:34
  • I will have a zoom class and possible other classes that are going to use the movieclip being panned so I don't think I will be able to have it extend all of those different elements. – Sean Jun 13 '12 at 12:25

1 Answers1

1

Create a singleton class that manages changes to the stage called StageManager and initialize it by passing it a reference to the stage:

//StageManager.as

import flash.display.Stage;

public class StageManager()
{
    //publicly accessible singleton instance
    public static var instance:StageManager = new StageManager();

    private var m_stage:Stage;

    //I'm using a custom getter and setter in just in case you need perform some other 
    //initialization when the stage gets set...
    public function set stage(stg:Stage):void
    {
        m_stage = stg;
    }

    public function get stage():Stage
    {
        return m_stage;
    }

}

Then somewhere else, like you main controller class:

StageManager.instance.stage = this.stage; 

Now you can access the stage and its properties globally through the StageManager:

var stageW:int = StageManager.instance.stage.stageWidth; 

This way, not just you Pan class, but anything else down the road can access the global stage any time it needs to. Pretty cool, huh?

As for how to design your Pan class, I agree with @The_asMan - extend MovieClip. Unless, that is, one Pan instance will be controlling several MovieClip instances, then it would probably make more sense to have it as its own thing (the way you describe above).

Ian
  • 3,806
  • 2
  • 20
  • 23
  • As of right now, the Pan class will only be controlling one other movieclip but future additions to my program might have it functioning for other clips as well so that is why I wanted to have it separate. – Sean Jun 13 '12 at 12:24
  • I tried to implement the code and I am getting errors. It says stage was not found or was not a compile time constant in the stageManager class. I think it need to pass stage to that from the main class. – Sean Jun 13 '12 at 12:47
  • I got it to work. I needed to add 'import flash.display.Stage;' to the StageManager class. – Sean Jun 13 '12 at 12:52
  • Glad you got it figured out! And yep, it would be a good idea to import the `Stage` class ;) I'll make an edit to the answer to include that line. – Ian Jun 13 '12 at 23:55