My use of virtual function overrides - 2D platformer tiles

A short while ago, virtual functions came in very handy when I was creating a 2D platformer tilemap. The map had certain tiles that crumbled away when the player touched them, so they were different from the other tiles.

I created a new crumbling tile class that inherited from the base tile, and then I overrode the draw method to include a crumbling animation as needed. This allowed me to use runtime polymorphism when rendering the platformer level - all tilemap tiles were contained within a base tile array, and I was able to iterate through and call the draw method on all the tiles without having to worry about whether or not they were the derived crumbling tile class!

4 Likes

Privacy & Terms