Skip to main content
Participant
January 12, 2013
Answered

What's difference between constructor and as operator?

  • January 12, 2013
  • 1 reply
  • 367 views

Hi, all~

Sometimes a property returns value as "super" as the class related. For a simple example: a child call its parent MovieClip to .stop(); However, this.parent property returns a DisplayObjectContainer which has no such method. At this time, I like to use (parent as MovieClip).stop(); to "treat him as" a MovieClip, because I think its already a MovieClip, there's no need to "convert". But I noticed that many people and many examples in the reference and documents use the constructor, like: MovieClip(parent).stop(); even the constructor do not receive a parameter. I'm wondering what's the difference between them, and which is better?

Am I entanglement? Thanks~~~~

This topic has been closed for replies.
Correct answer kglad

that's not using the constructor.  nothing is being created or constructed when you use MovieClip(child.parent).

you are casting the child.parent as a MovieClip.  that can only be done if child.parent already is a MovieClip and the only reason you need to do that is because the flash compiler doesn't have a terrific memory.  it easily forgets that a child with a MovieClip parent is a MovieClip. 

the compiler knows every child has a DisplayObjectContainer parent but it often forgets if that parent is a MovieClip or a Sprite or a Loader or the stage etc.  so, if you fail to cast the parent as a MovieClip the compiler can (erroneously) complain that you're using an illegal method and/or you're implicitly casting a DisplayObjectContainer as a MovieClip.

explicitly casting as a MovieClip makes the compiler happy.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
January 12, 2013

that's not using the constructor.  nothing is being created or constructed when you use MovieClip(child.parent).

you are casting the child.parent as a MovieClip.  that can only be done if child.parent already is a MovieClip and the only reason you need to do that is because the flash compiler doesn't have a terrific memory.  it easily forgets that a child with a MovieClip parent is a MovieClip. 

the compiler knows every child has a DisplayObjectContainer parent but it often forgets if that parent is a MovieClip or a Sprite or a Loader or the stage etc.  so, if you fail to cast the parent as a MovieClip the compiler can (erroneously) complain that you're using an illegal method and/or you're implicitly casting a DisplayObjectContainer as a MovieClip.

explicitly casting as a MovieClip makes the compiler happy.