Player object inside the scene

Hi!..

I don´t really like to guess to use a blind development way to follow… so i ask you more experienced programmers…

Do you use Composing or Inheritance in your development?..
I have done my first two scenes (Menu and a simple non playable comic view prologue story)…
The third scene is the first gameplay so of course need to create my player.

But a player could be a simple sprite that reacts to input, that does not requires an external custom type, say Player.h, this is the way you follow or do you create your own Player.h class with jump, crouch, run and talk member functions?..

Cocos was designed around sub-classing. That being said I do both subclassing and composition these days. It works well for how I write games.

For me Player would be a class with all the members that make player do what it needs to do for your game.

Are you familiar with design patterns?

2 Likes

Hi. I use Inheritance for simple classes and composition for bigger project (AI component, Physics Component , etc.)

3 Likes

ok, so i can combine them…
i will use inheritance for my project so…

thanks both.

I create custom nodes for almost everything unless it literally only is using the variables and functions of the base cocos node I would subclass from.

That being said, Some subclasses can be compositions with others:
Ex:

class BaseSpaceShip: public Sprite
class HeroShip1: public BaseSpaceShip
class HeroShip2: public BaseSpaceShip
class Shadow: public Sprite

The BaseSpaceShip class could contain 1 or more of the Shadow class inside it etc which now are also inherited by the more customized spaceship subclasses.

2 Likes

Of course! How did not get it?
My player can inherit from sprite class

This should be my inheritance so…

ControllableType : public Sprite
VehicleType : public ControllableType
HumanType : public ControllableType
Starship : public VehicleType
Henry : public HumanType
John : public HumanType

For scenes i was thinking something like this:

ScnMaster : public Scene
ScnUIType : public ScnMaster
ScnGamePlayType : public ScnMaster
ScnMainMenu : public ScnUIType
ScnForest : public ScnGamePlayType

May be?
Thanks

Edit: And I could say it should be a good idea to use Singleton for save/load and store player stats isn’t ?

2 Likes

Yes, I generally make singletons for all my high level processes.

ex:
DatabaseManager
ChatCache
UIState

And yes on subclassing the cocos Sprite class, generally, look for the cocos node that at base has what you need (assuming you need some cocos attributes for the class), and build on top. ie, you can use the Ref class if you need an automatic reference count on some basic data classes. You can use Node if you need something generic for adding to the node hierarchy, you can use Sprite if the node will be a sprite at its base, Layer …, Scene …, Renderer, etc etc

1 Like

All clear thanks!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.