结着写了;
创建一个金币展示板层,在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();//监听一切活动/变化
检查了一遍,好像没有漏写什么。
可以实现以下效果:
分享到:
相关推荐
这是我重新弄的cocos2d-x-3.0的类图.之前别人兄台弄的,有些不全面,有些地方错误.我这个可以说是最新的了.每个类添加了中文的详细注解,同时也添加了中文的类名称翻译.这样对cocos2d-x-3.0的框架比较好上手. 有兴趣的...
coco2d-x2.03 chm 文档.
详细介绍 基于coco2d-x 3.0开发的2048,内容详细,代码整洁,适合学习。
cocos2d-x 2.x action集合(详细注释分类)
引擎:Quick-Cocos2dx-Community 3.6.1 BulletWar游戏源码coco2d-x开发 lua 仅作学习交流使用!
这是我自己写的坦克大战 ,基于cocos2d-x 3.8 ,还没有写完,先把写的传上去和大家分享一下
基于coco2d-x引擎,用C++语言,自己写的一个flappy bird。
coco2d-x场景切换具体办法,此文档用于解决新手遇到场景切换的问题,
ActionTimeline动画的使用,二、修改动画,关键帧事件,创建多个Node对象,异步加载动画Armature,创建动画并显示,设置骨骼动画的速度,批量添加怪物,实现动画的事件...在场景中获取粒子效果等等COCOS2D-X使用教程尽在其中
UIView和Coco2d-x场景之间的相互切换实现,只是项目中的ios文件夹
physicsEditor 编辑 coco2d-x解析 box2d debugdraw 实例工程 zip for mac
coco2d-X 游戏资源合集 适合新手练手学习之用,不可以做商业用途。 coco2d-X 游戏资源合集 适合新手练手学习之用,不可以做商业用途。
《我所理解的Cocos2d-x》针对最新的 Cocos2d-x 3.x版本,介绍了Coco2d-x游戏引擎的基本架构、渲染机制,以及各个子模块的功能和原理,并结合OpenGL ES图形渲染管线,深入探讨了游戏开发中涉及的相关图形学的知识,...
因为最近在学cocos2d-x,找了半天在网上也找不到一个离线的文档,于是自己抽空做了一个,全部内容提取自cocos2d-x中文官网的文档页http://cn.cocos2d-x.org/article 目前只提取了cocos2d-x部分内容。因为内容比较多...
使用cocos2d-x3.2编写的贪吃蛇游戏,包含主界面,游戏界面,帮助界面,界面的切换与游戏的实现。适用新手学习cocos2d-x3.2引擎。
Coco2d-x权威指南,这本书对Coco2D-x剖析得比较清晰,如果需要您就下载吧!有什么问题给我留言
基于cocos2d-x 3.8 版本编写的flappy bird ,对于初学者很有帮助。分享给大家,希望能帮助到你
《我所理解的Cocos2d-x》针对最新的 Cocos2d-x 3.x版本,介绍了Coco2d-x游戏引擎的基本架构、渲染机制,以及各个子模块的功能和原理,并结合OpenGL ES图形渲染管线,深入探讨了游戏开发中涉及的相关图形学的知识,...
co co s2 d-x
cocos2d-x 文档 本地查询 方便使用