Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

What's difference between constructor and as operator?

Community Beginner ,
Jan 12, 2013 Jan 12, 2013

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~~~~

TOPICS
ActionScript
345
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 12, 2013 Jan 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 paren

...
Translate
Community Expert ,
Jan 12, 2013 Jan 12, 2013
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines