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值得好好研究学习。

下一篇介绍下游戏中的设计模式——托付模式







最新文章

  1. ASP.NET Core 之 Identity 入门(三)
  2. python基础知识---操作文件
  3. hibernate进行多表联合查询
  4. Complete The Pattern #6 - Odd Ladder
  5. 问题:关于坛友的一个定时重复显示和隐藏div的实现
  6. HDU 1429 胜利大逃亡(续)(DP + 状态压缩)
  7. uboot之board.c源码分析
  8. ORACLE DATABASE 10G FALSHBACK 知识整理
  9. Java阶段性测试--知识点:数组,面向对象,集合、线程,IO流
  10. selenium页面元素操作(简易版)
  11. java学习之—栈
  12. gitlab的配置
  13. 何时使用SUM()与SUMX()
  14. pandas read_csv 读取中文列标题文件报错
  15. Android Context完全解析,你所不知道的Context的各种细节
  16. 134. Gas Station (Array; DP)
  17. C++内存布局(1)-让new出的两个变量在堆上的地址连续
  18. es6 Reflect对象详解
  19. protoc-gen-go: error:bad Go source code was generated: 163:6: illegal UTF-8 encoding (and 2915 more errors)
  20. 自定义数据类型写入SequenceFile并读出

热门文章

  1. Python 字典(dict)操作(update)
  2. Mac OS X10.9安装的Python2.7升级Python3.4步骤详解
  3. Mysql数据类型(一)
  4. Navcat12 for mysq 激活工具
  5. Android网络编程随想录(2)
  6. [lua] future模式*协程
  7. 【Oracle】ORA-38171: Insufficient privileges for SQL management object operation
  8. BigDataMini导论
  9. tomcat_logs
  10. gulp给文件后添加md5时间戳