tags:

views:

67

answers:

1

I am curious about code in my action script project:

public var _p:Object ...

in a function:

public function WObject(pp:MovieClip) { _p = pp; _p.Play(); }

The Play() function is in MovieClip but not Object, can action script allow such syntax?

A: 

Since any AS3 object is a child of Object, downcasting a MovieClip to Object is possible. This is possible due to the dynamic nature of AS3 objects (since AS3 is based on ECMAScript) which would allow you to invoke methods such as play() on an Object that contains a MovieClip without having the compiler/player throw sharp objects at you.

Having said the above, it is generally a good practice not to downcast in such a manner unless absolutely necessary since this tends to make code harder to understand and thus maintain.

Other considerations are compile-time type checking and code-hinting (argument hints, code completion, etc). These would not be available to you should you choose to downcast to Object.

Lior Cohen
Thanks for you answer, and I have another question regarding that. Is the action script totally dynamic, this means the file can pass the compile even the compiler can't find the definition of a method, like:public var _p:Object ...in a function:public function WObject(pp:MovieClip) { _p = pp; _p.PlayB(); }The PlayB is not a member of MovieClip, but a member of a class derived from MovieClip. In run time I will pass an instance of the derived class to WObject. Can this code pass the action script compiler?
Bin Chen
Yes. According to what you've described, it should work just the same. Try it and see.
Lior Cohen
Wow that's cool
Bin Chen