1.重写了CCSprite类
头文件:
/* 文件名: GameSprite.h 描 述: 游戏中定义的一些精灵类 创建人: 郝萌主 (博客:http://blog.csdn.net/haomengzhu) 创建日期: 2013.12.05 */ #ifndef __GAMESPRITE_H__ #define __GAMESPRITE_H__ #include "cocos2d.h" USING_NS_CC; enum { kBackground,//背景层 kMiddleground,//中间层 kForeground //最上层 }; enum { kSpritePlayer, kSpriteTerrain, kSpriteBlock, kSpriteChimney, kSpritePuff }; class GameSprite : public CCSprite { protected: CCSize _screenSize; public: //定义变量,并且直接定义默认的get/set方法
/* CC_SYNTHESIZE注解
CC_SYNTHESIZE(类型名,变量名,get/set名)
CC_SYNTHESIZE宏的作用,声明一个变量和其get/set方法
*/
CC_SYNTHESIZE(CCPoint, _nextPosition, NextPosition); CC_SYNTHESIZE(float, _width, Width); CC_SYNTHESIZE(float, _height, Height); CC_SYNTHESIZE(CCPoint, _vector, Vector); GameSprite(void); ~GameSprite(void); //内联虚函数,只是一个接口 inline virtual void place () { this->setPosition(_nextPosition); }; inline virtual float radius () { return _width * 0.5f; } inline void setSize() { _width = this->boundingBox().size.width; _height = this->boundingBox().size.height; }
/*
Sprite类的boundingBox 方法:
CCRect | boundingBox (void) |
Returns a "local" axis aligned bounding box of the node. More... |
*/ }; #endif // __GAMESPRITE_H__
GameSprite.cpp
/* 文件名: GameSprite.cpp 描 述: 游戏中定义的一些精灵类 创建人: 郝萌主 (博客:http://blog.csdn.net/haomengzhu) 创建日期: 2013.12.05 */ #include "GameSprite.h" USING_NS_CC;
/*
初始化GameSprite类中的_vector和_screenSize
*/ GameSprite::GameSprite(void) :_vector(ccp(0,0)) ,_screenSize(CCDirector::sharedDirector()->getWinSize()) { } GameSprite::~GameSprite(void){}