- Cocos2d-x Game Development Blueprints
- Karan Sequeira
- 601字
- 2021-07-16 13:47:58
Creating your own Sprite
As you go about developing different types of games, you may find that you need to store some more information regarding the entities or objects in your game. Such a situation has arisen for this particular game as well. For SpaceCraze, we will need to manipulate the size of each of our entities' sprites: player, enemy and brick. In addition to size, we will also need to manipulate the boundingBox
function. Why do we need to change the bounding box you wonder? Just take a look at a shot of the player's sprite frame shown here:

Notice how the texture here includes a shadow for styling. This would cause the bounding box for the player's sprite to include the shadow. Consequently, our collisions would look visually incorrect with the player getting shot without the enemy bullet actually making contact with the body of the player's ship.
So without further ado, let's get to the header file of the class that we shall conveniently name CustomSprite
:
#ifndef CUSTOM_SPRITE_H_ #define CUSTOM_SPRITE_H_ #include "GameGlobals.h" class CustomSprite : public CCSprite { public: CustomSprite() : size_(CCSizeZero) {} virtual ~CustomSprite(); // returns and autorelease CustomSprite static CustomSprite* createWithSpriteFrameName(const char* frame_name); // override CCSprite's boundingBox method virtual CCRect boundingBox(); // add a customized CCSize used for the boundingBox CC_SYNTHESIZE(CCSize, size_, Size); }; #endif // CUSTOM_SPRITE_H_
You can clearly see how CustomSprite
publicly inherits from CCSprite
. Within the header file, we have the constructor and destructor. You can see how the constructor initializes a variable called size_
to CCSizeZero
. I am sure you're looking for this variable within the class, but are you looking close enough?
The size_
variable is wrapped neatly within the CC_SYNTHESIZE
macro function at the bottom of the header file. Jump to the definition of CC_SYNTHESIZE
and you will see that the macro simply declares a protected
variable of a given type (first parameter) with the given name (second parameter), and it goes one step ahead to create public accessor
/getter
and mutator
/setter
functions of the specified name (third parameter).
Next, we have a static
function that returns a pointer to CustomSprite
and has a very familiar name. That's right, createWithSpriteFrameName
belongs to the CCSprite
class and we will be borrowing the same name to create our CustomSprite
class. This function will receive the name of the sprite frame that CustomSprite
will use.
Last but not the least, you can see the virtual
function boundingBox()
that we will be over-riding in our CustomSprite
class. Let's take a look at the implementation of the class in CustomSprite.cpp
file:
#include "CustomSprite.h" CustomSprite::~CustomSprite() {} CustomSprite* CustomSprite::createWithSpriteFrameName( const char* frame_name) { CustomSprite* sprite = new CustomSprite(); if(sprite && sprite->initWithSpriteFrameName(frame_name)) { sprite->autorelease(); return sprite; } CC_SAFE_DELETE(sprite); return NULL; } CCRect CustomSprite::boundingBox() { // return bounding box based on our own size_ variable return CCRectMake(m_obPosition.x - size_.width/2, m_obPosition.y - size_.height/2, size_.width, size_.height); }
We started the staticcreateWithSpriteFrameName
function by creating a new CustomSprite
object. We then called the initWithSpriteFrameName
function of CCSprite
, which returns either true
or false
based on successful or unsuccessful initialization, respectively. If all is well, we mark the CustomSprite
object as autorelease
and return it. If all is not well, we delete the CustomSprite
object and return NULL
. The CC_SAFE_DELETE
function is yet another useful macro. You will find a multitude of such macros in the CCPlatformMacros.h
file located inside cocos2dx/platform/
.
Finally, we have the over-ridden boundingBox
function. As you may have guessed, we create and return a custom CCRect
based on the size_
value of the CustomSprite
. All right! With a customized CCSprite
on our shoulders, we can begin creating our crazy space game world.