cocos2d-x 3.0 经常使用对象的创建方式
cocos2d-x 3.0 中全部对象差点儿都能够用create函数来创建,其它的创建方式也是有create函数衍生。
以下来介绍下create函数创建一般对象的方法,省得开发中常常忘记啥的。
1、精灵Sprite的4种创建方式
(1)依据图片资源路径来创建
//依据图片路径来创建
auto sprite1 = Sprite::create(filepath);
//依据图片路径来创建,并设置要显示的图片大小
auto sprite2 = Sprite::create(filepath,Rect(0,0,width,height));
(2)依据plist文件里的frame name创建,图片名称前要加#符号来区分
//參数:帧名字,frame name
auto sprite = Sprite::create('#ball.png');
(3)依据缓存plist中的sprite frame来创建,这样的用的比較多
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("loadingAndHP.plist","loadingAndHP.png");//载入图片资源作为缓存
//从缓存图片中依据图片名字来创建
auto loading_bk=Sprite::createWithSpriteFrameName("loading_bk.png");
(4)依据纹理texture创建
//依据纹理图片来创建
auto batch = SpriteBatchNode::create("Images/grossini_dance_atlas.png", 1);//载入缓存纹理
//依据纹理来创建精灵
auto sprite = Sprite::createWithTexture(batch->getTexture());
//依据纹理来创建精灵,并设置显示区域大小
auto sprite = Sprite::createWithTexture(batch->getTexture(), Rect(x, y, width, height));
2、文字LabelTTF的创建方式
(1)依据字体、大小等多參数创建
auto center = LabelTTF::create("hello cocos2d-x",//要显示字符串
"Paint Boy",//字体
32,//字号
Size(s.width/2,200),//显示的宽和高
TextHAlignment::CENTER,//显示的位置,定位
TextVAlignment::TOP);
(2)依据自己定义对象FontDefinition来创建
FontDefinition shadowTextDef;
shadowTextDef._fontSize = 20;//字号
shadowTextDef._fontName = std::string("Marker Felt");//字体 shadowTextDef._shadow._shadowEnabled = true;//设置是否有阴影
shadowTextDef._shadow._shadowOffset = shadowOffset;
shadowTextDef._shadow._shadowOpacity = 1.0;//设置透明度
shadowTextDef._shadow._shadowBlur = 1.0;
shadowTextDef._fontFillColor = tintColorRed; // shadow only label
auto fontShadow = LabelTTF::createWithFontDefinition("Shadow Only Red Text", shadowTextDef);//依据自己定义的字体创建要显示的内容
3、对于3.0中SpriteBatchNode,不鼓舞使用,文档看这里点击打开链接,在此不再赘述SpriteBatchNode的使用方法。
4、帧动画创建方式
通过多张图片文件来创建一个动画。
Animation* animation = Animation::create();
animation->setDelayPerUnit(1.0 / fps);
for (int i = 0; i <= count; i++) { const char *filename = String::createWithFormat(fmt, i)->getCString();
SpriteFrame* frame = SpriteFrameCache::getInstance()->spriteFrameByName(
filename);
animation->addSpriteFrame(frame);
}
Animation是由Sprite frame帧组、单个frame延时,持续时间等组成的,它是一组“数据”,而Animate是一个Action,它是基于Animation对象创建的。
5、序列帧动画Sprite sheet animation
(1)通过.plist文件来创建
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(filename);//载入.plist文件到缓存中
AnimationCache* cache = AnimationCache::getInstance()->addAnimationsWithFile(filename);//依据动画.plist文件来创建动画缓存
Animation* animation = cache->animationByName(filename);//直接从缓存中创建动画
Animate* animate = Animate::create(animation);//生成动画动作
(2)通过数组来创建
Array* animFrames = Array::createWithCapacity(15);
char str[100] = {0};
for(int i = 1; i < 15; i++)
{
sprintf(str, "grossini_dance_%02d.png", i);
SpriteFrame* frame = cache->spriteFrameByName( str );
animFrames->addObject(frame);
}
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.3f);
好了,这篇简介了游戏中常常要用到的对象的创建方式,对于游戏中的其它元素。能够參考游戏源代码的demo中的创建方式,这个demo值得好好研究学习。
下一篇介绍下游戏中的设计模式——托付模式
cocos2d-x 3.0 经常使用对象的创建方式的更多相关文章
- (转)ASP.NET Mvc 2.0 - 1. Areas的创建与执行
转自:http://www.cnblogs.com/terrysun/archive/2010/04/13/1711218.html ASP.NET Mvc 2.0 - 1. Areas的创建与执行 ...
- hibernate4.0中SessionFactory的创建
创建SessionFactory 首先创建Configuration对象,主要方式是: new Configuration().configure() 默认情况下Hibernate会去classPat ...
- 高屋建瓴 cocos2d-x-3.0架构设计 Cocos2d (v.3.0) rendering pipeline roadmap(原文)
Cocos2d (v.3.0) rendering pipeline roadmap Why (the vision) The way currently Cocos2d does rendering ...
- Android(Lollipop/5.0) Material Design(四) 创建列表和卡片
Material Design系列 Android(Lollipop/5.0)Material Design(一) 简单介绍 Android(Lollipop/5.0)Material Design( ...
- 在java中使用solr7.2.0 新旧版本创建SolrClient对比
在Java中使用solr 版本7.2.0 solrj已经更新到了7.2.0,新版本solr获取SolrClient的方式也和之前旧版本有所不同 solr6.5开始不推荐直接使用HttpSolrClie ...
- 菜单(Menu)的三中创建方式——Android开发之路2
菜单的三种创建方式 一.OptionsMenu---选项菜单 Android应用中的菜单默认是隐藏的,只有当用户点击手机上的MENU键,系统才会显示菜单.这种菜单叫做选项菜单(Options Menu ...
- AlertDialog的六种创建方式
AlertDialog的六种创建方式 创建AlertDialog的步骤: 1.创建AlertDialog.Builder对象 2.调用Builder对象的setTitle方法设置标题,setIcon方 ...
- java多线程总结一:线程的两种创建方式及优劣比较
1.通过实现Runnable接口线程创建 (1).定义一个类实现Runnable接口,重写接口中的run()方法.在run()方法中加入具体的任务代码或处理逻辑. (2).创建Runnable接口实现 ...
- 帧动画的创建方式 - xml方式
废话不多说,先看东西 创建帧动画1 - xml方式 帧动画的创建方式主要以下2种: * 用xml创建动画: * 用代码创建动画: 本文内容主要关注 xml文件 创建帧动画的方式 xml文件 ...
随机推荐
- 【TYVJ1936】【Clover杯NOIP模拟赛IV】太空战队
[问题描述] 在社会经济高速发展的今天,借助高科技手段,组建太空战队的愿望就快实现了. 战队属下有N个航天员.作为空军选拔上来的佼佼者,每个航天员都有与生俱来的骄傲——他们每个人都认为自己是最强的或者 ...
- JS函数种类详解
1. 普通函数1.1 示例 1 2 3 function ShowName(name) { alert(name); } 1.2 Js中同名函数的覆盖 在Js中函数是没有重载,定义相同函数名.不同 ...
- TexturePacker贴图打包工具
1.该软件是收费的,不过对于这么一款实用的工具来说,物有所值,下载地址 http://www.codeandweb.com/texturepacker 2.openGL载入纹理图片的时候,所用内存是会 ...
- caffe 参数介绍 solver.prototxt
转载自 http://blog.csdn.net/cyh_24/article/details/51537709 solver.prototxt net: "models/bvlc_alex ...
- Java电子书下载地址
http://www.itpub.net/search.php?searchid=1660&orderby=lastpost&ascdesc=desc&searchsubmit ...
- android黑科技系列——Apk的加固(加壳)原理解析和实现
一.前言 今天又到周末了,憋了好久又要出博客了,今天来介绍一下Android中的如何对Apk进行加固的原理.现阶段.我们知道Android中的反编译工作越来越让人操作熟练,我们辛苦的开发出一个apk, ...
- textarea 自适应高度
试了好多方法,包括百度了好多.一旦接口获取的内容,就不好用了.有时候就是脑袋转不过来,想了好久的方法居然那么简单,然后,脑洞大开,忽然想到还可以这样弄, 很简单,两句话 var textareaHei ...
- Linux 安装MySQL5.7.18
https://dev.mysql.com/downloads/mysql/Linux-Generic md5sum mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz ...
- 从Spark1.6到Spark2.1,Logging该何去何从
大家都知道spark 1.6.0版本比较稳定,也比较流行. 我们项目组也是,最初用的就是这个版本. 这段时间,项目组引入spark 2.1.0版本,我想尝尝鲜. Pom中刚刚换了dependency马 ...
- 三维建模:方法之CSG与B-Rep比较
源自于百度百科:实体造型--- 三维模型特征表示方法: 计算机中表示三维形体的模型,按照几何特点进行分类,大体上可以分为三种:线框模型.表面模型和实体模型.如果按照表示物体的方法进行分类,实体模型基本 ...