I'm having a little confusion when distinguishing this in an as3 file
lets say I have a main stage linked to a Main.as
then whats the differenence between
this.addChild()
stage.addChild()
also what typ开发者_如何学编程e 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?
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):
addChild(foo);
this.addChild(foo);
The following is an example of a document class:
package
{
import flash.display.Sprite;
import flash.events.Event;
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.
精彩评论