17 game development in Cocos2D-x

Source: Internet
Author: User

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:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.