结着写了;
创建一个金币展示板层,在DollarDisplayLayer.h中
#pragma once
#include "e:\cocos2d-x\cocos2d-x-2.2\cocos2d-x-2.2\cocos2dx\layers_scenes_transitions_nodes\cclayer.h"
#include "cocos2d.h"
class DollarDisplayLayer :public cocos2d::CCLayer
{
public:
int _dollar;//金币展示板;
cocos2d::CCString* _dollarStr ;//经过类型转换后得金币;
cocos2d::CCLabelTTF* _dollarLabel ;//金币展示板
DollarDisplayLayer(void);
~DollarDisplayLayer(void);
CREATE_FUNC(DollarDisplayLayer);
virtual bool init();
void displayDollarLabel();//声明金币展示板函数
};
在.cpp中
#include "DollarDisplayLayer.h"
USING_NS_CC;
DollarDisplayLayer::DollarDisplayLayer(void)
{
this->_dollar =225;
this->_dollarStr =NULL;
}
DollarDisplayLayer::~DollarDisplayLayer(void)
{
}
bool DollarDisplayLayer::init()
{
if(!CCLayer::init())
{
return false;
}
this->displayDollarLabel();
return true;
}
//定义金币展示板函数
void DollarDisplayLayer::displayDollarLabel()
{
//动态创建金币展示板
this->_dollarLabel = CCLabelTTF::create();
this->_dollarStr =CCString::createWithFormat("dollar:$%d",this->_dollar);
this->_dollarLabel->setString(this->_dollarStr->m_sString.c_str());//动态金币展示板转化完毕
this->addChild(this->_dollarLabel);
CCSize winSize =CCDirector::sharedDirector()->getWinSize();
this->_dollarLabel->setPosition(ccp(2*winSize.width/7, 8*winSize.height/9));
this->_dollarLabel->setFontSize(26);
this->_dollarLabel->setColor(ccc3(0,0,0));
}
然后我们把这个层加到主游戏层中去;
在GameLayer.h中:
#include "DollarDisplayLayer.h"
DollarDisplayLayer* _dollarDisplayLayer;
void initDollarDisplayerLayer();
在GameLayer.cpp中
在构造函数中加入以下代码:
this->_dollarDisplayLayer =NULL;
//定义金币展示层函数
void GameLayer::initDollarDisplayerLayer()
{
this->_dollarDisplayLayer =DollarDisplayLayer::create();
this->addChild(this->_dollarDisplayLayer);
}
把定义的initDisplayLayer()加入到bool GameLayer::init()方法中去;
this->initDollarDisplayerLayer();//初始化金币展示板
下面我们要做的就是时刻监听金币的变化,和后来其他事件的变化,所以需要在GameLayer主层中声明udate(float dt)方法
在GameLayer.h中
void update(float dt);//监听所有变化的事物
在GameLayer.cpp中:
void GameLayer::update(float dt)
{
//监听金币变化
this->_dollarDisplayLayer->_dollarStr =CCString::createWithFormat("dollar:$%d",this->_dollarDisplayLayer->_dollar);
this->_dollarDisplayLayer->_dollarLabel->setString(this->_dollarDisplayLayer->_dollarStr->m_sString.c_str());
}
在bool GameLayer::init()中使用scheduleUpdate()
scheduleUpdate();//监听一切活动/变化
检查了一遍,好像没有漏写什么。
可以实现以下效果: