欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 运维知识 > windows >内容正文

windows

cocos2d-尊龙游戏旗舰厅官网

发布时间:2025/1/21 windows 39 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 cocos2d-x2.2.3学习笔记5(ui系统) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

前言:

1.字体

2.标签

3.菜单

4.进度条

5.计时器


cocos2d-x中得ui控件没有几个。在游戏制作的过程中也不须要什么ui。即使有些复杂的ui,那都得我们自己来封装的。比方。关卡选择。

它不像做ios或android。winform一大堆的ui控件

     

  以下我们来介绍一下比較经常使用的ui


     1.字体

     cocos2d-x中有三种字体。ttf/bmfnt/arial,

   它们都是cclable下得一个子类,cclable看名字当然知道是标签了,所以我们把标签和字体一起来解说


ok,我们先来看看ttf的字体,cclablettf。

我们在c/windows/fonts文件夹下能够看到非常多ttf的字体,这是我们windows系统中自带的字体。苹果手机也有,这样的字体我个人赶脚(感觉)是非常丑,我比較喜欢bmfont的字体,这个我们就高速的过一下吧,知道这么创建即可了、

   我们新建一个项目,把init函数中多余的代码删了,然后写上我们自己的代码

bool helloworld::init() {//// 1. super init firstif ( !cclayer::init() ){return false;}ccsize visiblesize = ccdirector::shareddirector()->getvisiblesize();cclabelttf* ttf= cclabelttf::create("hello cocos2d-x","fonts/marker felt.ttf",21);ttf->setposition(ccp(visiblesize.width/2,visiblesize.height/2));this->addchild(ttf);return true; }执行,ok 我们输出了hello cocos2d-x在屏幕中心,这个ttf字体的create静态函数有四个重载。我们就用最简单的第四个就能够了,

看形參名字相信都应该知道每一个參数相应什么吧。这里不解释。。。


以下我们来看看另外一种字体。也是我比較喜欢的一种cclablebmfont

我们换成例如以下代码

bool helloworld::init() {//// 1. super init firstif ( !cclayer::init() ){return false;}ccsize visiblesize = ccdirector::shareddirector()->getvisiblesize();cclabelbmfont* bmfont=cclabelbmfont::create("fonttest","fonts/boundstestfont.fnt");bmfont->setposition(ccp(visiblesize.width/2,visiblesize.height/2));this->addchild(bmfont); #pragma region ttf/*cclabelttf* ttf= cclabelttf::create("hello cocos2d-x","fonts/marker felt.ttf",21);ttf->setposition(ccp(visiblesize.width/2,visiblesize.height/2));this->addchild(ttf); */ #pragma endregionreturn true; }
ok。美丽吧??你们执行报错??好吧。忘了还有资源文件没拷进去。待会源代码和资源我会打包上传的。

这里我们也是有最简单的方式创建。第二个參数是resources\fonts目录以下的一个fnt格式的文件。它相应一张图片,打开图片看看,这就是我们执行显示的字体,大家不用纠结fnt这么制作的,它有相应的工具生成。当然,图片还是的相关的美工来做。

我们在看看例如以下代码来制作点效果

bool helloworld::init() {//// 1. super init firstif ( !cclayer::init() ){return false;}ccsize visiblesize = ccdirector::shareddirector()->getvisiblesize();cclabelbmfont* bmfont=cclabelbmfont::create("fonttest","fonts/boundstestfont.fnt");bmfont->setposition(ccp(visiblesize.width/2,visiblesize.height/2));this->addchild(bmfont);ccactioninterval* jump = ccjumpby::create(0.5f, ccpointzero, 30, 1);ccaction* jump_rever = ccrepeatforever::create(jump);bmfont->getchildbytag(0)->runaction(jump_rever);return true; }这里ccjumpby和后面这行看不懂没关系,这是制作一个跳跃的动画。我们后面的章节会解说动画的,我们仅仅要来看看getchildbytag的方法,这表示获得tag为0的一个精灵,我们在创建字体的时候系统已经帮我们把每一个字母依照先后顺序加上了tag,这个有点像数组哈,这里我们得到字母f,然后让他运行跳跃的动作


今天有点晚了,我们加高速度。介绍计时器,明天在介绍菜单和进度条

#ifndef __helloworld_scene_h__ #define __helloworld_scene_h__#include "cocos2d.h"class helloworld : public cocos2d::cclayer { public:helloworld();// here's a difference. method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphonevirtual bool init(); // there's no 'id' in cpp, so we recommend returning the class instance pointerstatic cocos2d::ccscene* scene();void setstring(float ptime);// implement the "static node()" method manuallycreate_func(helloworld); private:float m_time; };#endif // __helloworld_scene_h__

helloworldscene.cpp


#include "helloworldscene.h"using_ns_cc;ccscene* helloworld::scene() {// 'scene' is an autorelease objectccscene *scene = ccscene::create();// 'layer' is an autorelease objecthelloworld *layer = helloworld::create();// add layer as a child to scenescene->addchild(layer);// return the scenereturn scene; }// on "init" you need to initialize your instance bool helloworld::init() {//// 1. super init firstif ( !cclayer::init() ){return false;}ccsize visiblesize = ccdirector::shareddirector()->getvisiblesize();cclabelbmfont* bmfont=cclabelbmfont::create("fonttest","fonts/boundstestfont.fnt");bmfont->setposition(ccp(visiblesize.width/2,visiblesize.height/2));this->addchild(bmfont,0,0);ccactioninterval* jump = ccjumpby::create(0.5f, ccpointzero, 30, 1);ccaction* jump_rever = ccrepeatforever::create(jump);bmfont->getchildbytag(0)->runaction(jump_rever);this->schedule(schedule_selector(helloworld::setstring),1);return true; } void helloworld::setstring(float ptime) {m_time =ptime;char stringtext[25] = {0};sprintf(stringtext, "%2.2f fonttest", m_time);cclabelbmfont* bmfont=(cclabelbmfont*)this->getchildbytag(0);bmfont->setstring(stringtext); } helloworld::helloworld() {m_time=0; } 以下我们来分析一下,首先我们添加了

this->schedule(schedule_selector(helloworld::setstring),1);

这就是计时器,表示1秒钟运行一次helloworld类中的setstring方法,

我们在setstring方法中做了非常easy的一件事,就是通过

cclabelbmfont* bmfont=(cclabelbmfont*)this->getchildbytag(0);获得我们的bmfont  然后bmfont->setstring(stringtext);

改动当前的文本。


计时器还有些重载的方法,不会的问问度娘吧,今晚就介绍到这,明天接着

总结:


ttf字体的创建

bmfont字体的创建

怎样获得指定下标的字体

       计时器的简单使用

怎样通过tag获得节点

        怎样改动字体文本

附源代码

总结

以上是尊龙游戏旗舰厅官网为你收集整理的cocos2d-x2.2.3学习笔记5(ui系统)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图