Skip to main content
DJ Gecko
Inspiring
October 3, 2008
Answered

Accessing display objects via the stage var

  • October 3, 2008
  • 3 replies
  • 681 views
I'm passing a stage reference to my class, but when I try to access a movieclip on the stage I get an error.

this gives me an error:
var mc = targetStage.myMovieClip;

Thanks!
This topic has been closed for replies.
Correct answer DJ Gecko
Thanks guys, you've given me a lot to think about.
wanna play my new game, just went live today : )
http://www.dorkbots.com/games/fishingchampion/index.html

Thanks for your help!

3 replies

Participating Frequently
October 3, 2008
This probably isn't the most "proper" way of doing things but, I just declare a static variable in my document class that holds a reference to the stage. That way I can access the stage from any object with MainClass.theStage.
This is my one and only "global" variable, but I find it to be a lot easier and cleaner than passing a stage reference to every DisplayObject I create.
October 3, 2008
I actually do this as well. I'm pretty certain it's "not proper," but passing variable references around can in my opinion make a really big mess. Usually I have a document class, and I expose important variables I consider being of global importance as static properties, and IMO that almost always includes the main timeline.
DJ Gecko
DJ GeckoAuthorCorrect answer
Inspiring
October 3, 2008
Thanks guys, you've given me a lot to think about.
wanna play my new game, just went live today : )
http://www.dorkbots.com/games/fishingchampion/index.html

Thanks for your help!

October 3, 2008
var myClass = new MyClass (this.stage);

That would be the same thing, this.stage would refer to the Stage instance. Each .swf has one Stage instance. In a Flash project, the stage has one child, the MainTimeline instance. MainTimeline is basically a big MovieClip, and it contains all your objects, scenes and timelines you create in Flash.

So, if you instantiate from the main timeline itself (either on a frame script or Document class) you are already inside MainTimeline, which is what you want to pass along a reference to, so you would pass in "this" as Colin said, or "root".

Colin mentioned that MovieClip(root) would work anyway; if I understand he is referring to the fact that every DisplayObject (such as MovieClips and SimpleButtons created in Flash) already have a "root" property that usually refers to the MainTimeline instance. However, your class "MyClass" does not extend DisplayObject so there is no root property available, which is why you need to manually pass in a reference to the root timeline.
Colin Holgate
Inspiring
October 3, 2008
Yes, that occurred to me (about still needing to pass in the reference because you're not extending displayobject). I was more commenting on it exactly matching a valid syntax for a similar feature. I'm never too keen on sample code like this either:

on whatever(event:Event){
trace(event.target);
}

The word "event" shows up in blue, even though it's just a normal variable in this case. Using reserved words as variable names could easily leave you a little confused about what a line means.


October 3, 2008
Ah, gotcha. Yes, using the word "root" might be confusing since there's already a property for DisplayObjects by that name.

Funnily enough, I usually use the variable name "evt" for event listeners for the same reason you brought up, but Adobe actually just released a best practice recommendation saying (among many other things) to always use "event" for the argument name in an event listener. Go figure!

http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions

quote:

Use event (not e, evt, or eventObj) for the argument of every event handler:

protected function mouseDownHandler(event:Event):void
October 3, 2008
The "stage" in Flash is not the same as the stage in ActionScript (confusing terminology.) The MovieClips you put on the main timeline in Flash are part of the MainTimeline class, which is usually accessed via "root."
DJ Gecko
DJ GeckoAuthor
Inspiring
October 3, 2008
so if I where to instantiate the class on the main timeline of the flash movie, would I write it like this?
var myClass = new MyClass (this.stage);

thanks!
Colin Holgate
Inspiring
October 3, 2008
I think you would just pass in (this), not (this.stage). Also, although abeall's example would work, using "root" as the variable is mildly confusing, because MovieClip(root) works anyway, as a way to get to the root of this swf (which may well be the same main time line you're trying to get to.