What's the different between var myObject : Sprite = new Sprite(); and var myObject : Sprite = new MovieClip();
One of them isn't right, and one of them is :)
var myObject:Sprite = new Sprite();
is fine, you're saying it's type Sprite (the bit after myObject:), and then calling the Sprite constructor after the '=': new Sprite();
In the other you're still saying it's type Sprite (myObject:Sprite), then calling the constructor of MovieClip when you say new MovieClip();
If you want to create a MovieClip:
var newMC:MovieClip = new MovieClip();
The difference is that
var myObject : Sprite = new Sprite();
declares a new variable of type Sprite
and assigns it with a new Sprite
object, while
var myObject : Sprite = new MovieClip();
declares a new variable of type Sprite
and assigns it with a new MovieClip
object.
MovieClip
inherits from Sprite
, so there is no problem with casting a MovieClip to Sprite.
There are some differences between MovieClip and Sprite. The major difference is that a MovieClip has a timeline and the Sprite doesn't.
MovieClips and Sprite are separate classes, albeit related to each other because MovieClip extends Sprite. As such, MovieClip has all the same capabilities that Sprite has, and it also adds more, mostly related to timeline animation (play(), stop(), gotoAndPlay(), addFrameScript() et c.)
Because MovieClip has all the same capabilities as Sprite, you can assign a MovieClip object to a variable typed as Sprite.
var myObject : Sprite = new MovieClip();
Sprite, however, does not share all of MovieClip's functionalities, so this does not work the other way around:
// Will not work
var myObject : MovieClip = new Sprite();
If you want to know the inheritance chain for a particular class, check out the documentation, e.g. for MovieClip: . You can see that it extends Sprite, which in turn extends InteractiveObject, and so on.
In language-agnostic terms, this is called inheritance, one of the strengths of which is polymorphism.