Archived

This topic is now archived and is closed to further replies.

bignester

Class question

Recommended Posts

I have two classes Sprite Game_Object I know I can do this both ways but which would be th best way to approach this problem. this sprite class just handles the animation of spites and what a current sprite is doing game object is any object in the game. Now my question is should game_object inherit from sprite or should game_object just have a sprite in the private section thanx for reading

Share this post


Link to post
Share on other sites
If Sprite is a object in the game, then shouldn''t Sprite inherit from game_object?

Ask yourself: What methods are exposed that are common to both classes? Then ask yourself, how many different classes share these common methods?

Share this post


Link to post
Share on other sites
As a rule of thumb, what you can do is what would be the best answer to the questions

"Game_Object" IS A "Sprite"
"Game_Object" HAS A "Sprite"

there is another question, "USES A", which can help you picture the dependencies between classes.

You are most likely to share sprites among different objects, so you don''t really want to inherit a single Game_Object to a single Sprite, it would be wasteful.

Also, I would think the game object will have a lot more than sprites, like sounds, controls, physical properties. It is not recommended (as far as I''m concerened) to multiple-inherit a "Game_Object" from a "Sprite" a "Sound_Object" and a "Physical_Object".

If you want to change the game object''s sprite (or sounds or A.I. properties) to something else, it would be a lot more trivial to have a pointer to sprite than using inheritance.

A Game_Object should have a sprite to render stuff, and a sound system to play sounds, ect... So "HAS A" would answer the problem better. So a Game_Object should have a pointer (or handle) to a sprite.

But you should sub-class the sprite class itself into different flavours of sprites, and obviously the game objects.

Share this post


Link to post
Share on other sites