After learning for so long, it is still a bit vague. Today we will make a simple summary of my previous knowledge:
First, let's take a look at our game's leading role: in development, we often do not set the leading role as an genie. Because the genie has other attributes, our main character is called CCsprite by a member variable:
Colleagues usually abstract a role entity because they share something in common:
class Entity :public CCNode,public ControlListener{public:Entity(void);~Entity(void); CCSprite *m_sprite;private:CController *m_control;public:bool BindSprite(CCSprite *sprite);virtual bool SetPlayerPosition(int x,int y) ;virtual CCPoint GetPlayerPosition() ;void SetControl(CController *control);};Here
CController is a class defined by us to implement role actions. We will introduce it later.
Entity::Entity(void){}Entity::~Entity(void){}bool Entity::BindSprite(CCSprite *sprite){m_sprite = sprite;this->addChild(m_sprite);return true;}void Entity::SetControl(CController *control){m_control = control;m_control->SetControlListension(this);}bool Entity::SetPlayerPosition(int x,int y){this->setPosition(ccp(x,y));return true;}cocos2d::CCPoint Entity::GetPlayerPosition(){return this->getPosition();}
With accumulation, we can define our own roles:
class Player :public Entity{public:Player(void);~Player(void);public:CREATE_FUNC(Player);virtual bool init();void run();};
Role Implementation:
Player::Player(void){}Player::~Player(void){}bool Player::init(){return true;}void Player::run(){CCSpriteFrameCache * freamCache = CCSpriteFrameCache::sharedSpriteFrameCache();freamCache->addSpriteFramesWithFile("run.plist","run.png");CCSpriteFrame *frame = NULL;CCArray *freamlist =CCArray::create();for (int i =1; i <= 15 ; i++){frame = freamCache->spriteFrameByName(CCString::createWithFormat("run%d.png",i)->getCString());freamlist->addObject(frame);}CCAnimation *anination = CCAnimation::createWithSpriteFrames(freamlist);anination->setLoops(-1);anination->setDelayPerUnit(0.08f);CCAnimate *animate = CCAnimate::create(anination);m_sprite->runAction(animate);}In this way, we have successfully created our game role.
Next, you only need to add our role to the init () function of the scenario. And run the run function.
Let's talk about it.
class CController :public CCNode{public:CController(void);~CController(void);void SetControlListension(ControlListener * listener);protected:ControlListener *m_listener;};
CController::CController(void){}CController::~CController(void){}void CController::SetControlListension(ControlListener * listener){m_listener = listener;}
He added a listener to the role to make the genie work, and designed another class:
class ControlListener{public:virtual bool SetPlayerPosition(int x,int y)=0;virtual cocos2d::CCPoint GetPlayerPosition()=0;};
This is all the code of our role: