this and stage in as3

I'm having a little confusion when distinguishing this in an as3 file

lets say I have a main stage linked to a

then whats the differenence between


also what type of object is "this"

lets say I create a class and pass in an instance of "this" so i can refer to stage from inside the MC

var mc:Derp = new Derp(this)

inside derp what is this? Is it of type generic object?

-------------Problems Reply------------

Inside members of a class, this refers to the current instance that the member function was called on (so it's always of the same type as the class). For example, if you had a class Twang with a member function func:

var twang1:Twang = new Twang();
twang1.func(); // Inside func(), "this" will refer to twang1

Flash first creates the stage (of type Stage) onto which everything else must be placed (either directly or indirectly). Then, it instantiates an instance of your document class, and places that on the stage as a child. So, this in your code can never refer to the stage, since you didn't write the Stage class.

Because the display tree is hierarchical in Flash, when you add something to a child object which is already on the stage (at least indirectly) it will appear on screen. So the difference between this.addChild() and stage.addChild() is which container the object will end up being inserted into.

When passing this to another object's constructor, it will be of the type of the class in which the method (which uses this) is defined; so in this case, it will be the type of the document class.

Instead of passing display object containers to other objects in order for them to add children to it, you can have you classes inherit from Sprite (or some other DisplayObjectContainer) and add children to themselves. Then, you can add the object you created (which will contain subobjects) to yourself (the document class instance), which is on the stage. This gives much better encapsulation, too.

Note also that if you want to add children to the stage directly you don't need to pass the stage around to those objects; they already have a stage property just for this purpose. The only tricky thing about that property is that it is null until the object is actually added to the stage; so, you need an event listener:

public class Car : Sprite {
public Car() {
this.addChild(new Wheel()); // Add to self

this.addEventListener(event.ADDED_TO_STAGE, populateStage);

private function populateStage():void {
stage.addChild(new RandomThingToAddToStage());

Finally, note that you don't need to specify this in most cases. The following two lines are equivalent (within a method of a class):


The following is an example of a document class:

import flash.display.Sprite;

public class Main extends Sprite
public function Main()
this.addChild(new Sprite());

addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

}// end function

public function onAddedToStage(e:Event):void
stage.addChild(new Sprite());

}// end function

}// end class

}// end package

Using the example above, the this keyword refers to the current instance, in this(no pun intended) case that would be the Main display object. When you call addChild() method on this, you are calling the addChild() method on the Main display object. At this point, when you add the new Sprite instance, you add it to Main display object or in other words this display object.

stage on the other hand is a public property of Main that stores a reference to the stage. When you add a new instance of Sprite using the stage reference's addChild() method, you add that instance to the stage not the Main display object.

To put it simply Main is a child display object of the stage. So when you do stage.addChild(new Sprite()), stage now has two child objects, the new Sprite instance and Main. However when you do this.addChild(new Sprite()) from within the Main class, you are refering to Main. So now the stage has a child display object Main, and Main has the new instance of Sprite as a child display object.

Category:flash Views:0 Time:2011-03-16

Related post

  • Loading object to stage with AS3 2009-08-06

    I am new to AS3 so please forgive me for asking something which is probably pretty straightforward to solve. I am trying to load an image onto the stage using an external AS3 class. I'm not sure if this is the best way to do it but I started off with

  • How to send a message to stage if AS3 FLVPlayback stream isn't found 2011-03-10

    I've successfully created an FLVPlayback component using the component library and it's playing the streaming video that I want it to. My question is, if that stream is not online or cannot be found, how would I go about displaying a message on the s

  • Centering Objects on Stage, and Auto-Scaling with Stage Resize AS3 2011-10-19

    I'm writing a web based flash app that's written entirely in AS3. I have objects on the screen as part of a GUI. I would like the user to be able to resize the window, or make it full screen. I would like everything to auto-scale with the resize, but

  • Accessing a symbol that is already on stage through as3 2012-01-04

    I have several symbols in the library (using Flash CS5), and all of these instances have a custom base class set with a method (lets say x()). I have the symbols already positioned on the stage using the IDE, and I wanted to access them through as3 a

  • Alternatives for registering Events on the stage in AS3? 2010-02-01

    I am programming a little software prototype as Flash/Actionscript3 application. Currently I registered some Events on stage - but it is cumbersome since stopPropoagation() needs to be used all the time. As Example: I am having a element shown via mo

  • Error loading Variables stage.loaderInfo - AS3 2010-04-06

    I have a Document class that loads variables from Facebook with the use of stage.loaderInfo var connect:FacebookConnectObject = new FacebookConnectObject( facebook, API_KEY, this.stage.loaderInfo ); But when I change the Document class (with another

  • How can I retrieve all MC in a Stage in AS3? 2011-08-04

    How can I retrieve all MC in a Stage? I want to call addEventListener for all MovieClip present in my stage and if it's possible in a selected frame number of my scenario Something like that pseudo code for(i=0; nbOfChild ; i++) if(stage.childAt(i) i

  • How do I access a movieClip on the stage using as3 class? 2011-09-02

    public class MyClass extends MovieClip { public function MyClass():void { my_mc.addEventListener(MouseEvent.CLICK, action); } private function action(e:MouseEvent):void { trace("cliked"); } } Timeline code var myClass:MyClass = new MyClass(); addChil

  • Showing an objects coordinates on stage with AS3..? 2011-10-22

    I am completely new to ActionScript so please understand that I do not know where to begin with this. I need to show a an objects coordinates on stage as it moves across the screen. Any help is much appreciated! thanks, Alex --------------Solutions--

  • recursively getting all children, grandchildren, etc of stage in as3 2011-11-03

    This is my first time using recursion in actionscript so I'm sure that there is something I'm not accounting for. What I'm trying to do is just iterate through stage children and trace out what the child is and the index. Here is some code. public fu

  • AS3: Add multiple buttons to stage with eventlistener? 2010-02-03

    I've got as far as adding multiple buttons to the stage with AS3 and giving them unique instance names, but now I have a problem. There is no limit on the amount of buttons that could be present, there could be one or one hundred so I can't hard code

  • How do I change the size and color of the stage in actionscript 3? 2009-10-26

    AFAIK, in AS2 it was "Stage.height = x;". --------------Solutions------------- From documentation of flash.display.stage in AS3: The Stage object is not globally accessible. You need to access it through the stage property of a DisplayObject instance

  • AS3 removing MovieClips in an array 2010-10-15

    Anybody any ideas on how to remove children from stage using AS3 if I store the reference to the objects in an array and they exist in different locations i.e they are not all children of the same parent? SomeArray.push(this); --------------Solutions

  • Actionscript 3 modify Label Component text 2009-10-29

    I'm trying to modify the text of a Label component that I already placed on the stage in as3. I'm having trouble getting the text to change though, it just stays the same based on the inital text I typed in. I'd like to eventually have the label cont

  • how can i change texts in the dynamic textFields in SimpleButton instance (button symbol)? 2010-05-06

    this may be a basic thing, but i couldn't find an answer by serching internet. I have created a simple button - Istance name = "btnsample" and there are two layers layer 0- button design with rollovers - layer 1- dynamic text field - instance name =

  • Not catching on key up event 2012-03-21

    I have a flash component TextInput within a movieclip, added to stage, through as3. The moment this mc is added to stage, it assigns keyboard event (key down and key up events), to the main stage (main app stage), and set main stage focus to main sta

  • 'glasspane' for as3 to intercept events to everything on stage? 2009-02-12

    Is there something like the java 'glasspane' in as3? The glass pane is useful when you want to be able to catch events or paint over an area that already contains one or more components. For example, you can deactivate mouse events for a multi-compon

  • flash AS3 how do I remove stage event listeners 2009-02-25

    I'm building an animation in flash and as3, I have a function in which I add a stage eventListener, stage.addEventListener(Event.ENTER_FRAME, setScrollPercent, false, 0, true); Since this event is set inside a function of a function, "two functions d

  • Adding an as2 swf to the stage in an as3 swf 2009-03-27

    I have a menu control (as2), that is completely self-contained and doesn't rely on any interaction with the parent (as3). The menu control does load an external xml file that is in the same directory as the swf. The menu control works fine when I run

Copyright (C), All Rights Reserved.

processed in 0.092 (s). 11 q(s)