Monday, 9 September 2013

Pass child object to function which requires parent object

Pass child object to function which requires parent object

I have a class Spaceship, which has the function with the following
signature: public function
CheckMissileHit(enemies:Vector.<Spaceship>):void
I inherited Spaceship class by EnemySpaceship. Now, my old code utilizes
the function above as follows: mPlayer.CheckMissileHit(mEnemySpaceships)
Originally, mEnemySpaceships was Vector.<Spaceship>, but now I want to
utilize some OOP concepts and created separate class called
EnemySpaceship.
But unfortunately, if I pass Vector.<EnemySpaceship> to the function,
which requires Vector.<Spaceship>, I'm screwed. One of the base principles
of OOP is polymorphism, so how can I produce it in AS3? Thanks.

No comments:

Post a Comment