I have a base class called CollidableObject and several inherited classes called Player , Enemy , InertObject , etc.
I'm trying to find an easy way to repeat all instances, so I initially created a list of type CollidableObject and put all the instances of the inherited classes there.
The thing is, due to the nature of polymorphism, when I do the following
foreach (CollidableObject CollidableObject in collidableObjects) { if (CollidableObject is Player) { CollidableObject.Draw(spriteBatch, testPlayerTexture); } // Then the same prodedure for each type of CollidableObject. // Might change to switch or something. }
The draw method that it calls is the general Draw method from the CollidbaleObject base, not the overridden / new from the Player / Enemy / inert object.
How do I get around this. Is there a way to iterate through a collection of objects from a single tree, but preserve their inherited type?
source share