Creating the GameObject class
Our game's elements are all physics bodies and will have sprites to represent them visually. It is a common practice to club these two entities together. Cocos2d-x has an inbuilt class to handle this known as PhysicsSprite
, but since this is our first physics game, we shall create our own class called GameObject
to do the job:
class GameObject : public CCSprite { public: GameObject() : game_world_(NULL), body_(NULL), type_(E_GAME_OBJECT_NONE) {} virtual ~GameObject(); // returns an autorelease GameObject static GameObject* create(GameWorld* game_world, const char* frame_name); // accessors & mutators inline b2Body* GetBody() { return body_; } inline EGameObjectType GetType() { return type_; } inline void SetType(EGameObjectType type) { type_ = type; } virtual void Update() { // update position of sprite based on position of body if(body_) { setPosition(ccp(WORLD_TO_SCREEN(body_->GetPosition().x), ...