I always don’t call super() when I extends Sprite.
But doesn’t not calling super() cause any problem?
Till now, I don’t have any problem and I have never seen code which call super() in constructor which class extends Sprite.
How about TextField?
I don’t have any problem about TextField, too.
How to know whether I should call super() or not?
If flash doesn’t detect a call to
super()in your child constructor then flash will implicitly callsuper()before your child’s constructor. So:So your child constructor essentially looks like this
So, no, omitting an explicit call to
super()will not usually adversely affect your child’s class.So why would you want to explicitly call
super()?The first reason is flash will only ever automatically generate a parameterless call to
super, meaning that if your parent classes constructor requires arguments, then you will need to explicitly call it with those arguments. If you omit thesuper(args...)call in this case, you will get a compiler error.Second, if even your parent has a parameter-less constructor, you can use
super()to control the order that the constructors execute. Flash will always insert the call before the childs constructor. So if you wanted to change that order. Thenwould do it in the opposite order. Or you could do:
Lastly, there is a very obscure way to not call your parents constructor by saying:
Because flash sees there is a call, it doesn’t insert one. However because its behind a
if (false)it never gets called, so the parent class never gets initialized.