cocos2d-x 3.0 中全部对象几乎均可以用create函数来建立,其余的建立方式也是有create函数衍生。html
下面来介绍下create函数建立通常对象的方法,免得开发中常常忘记啥的。数组
一、精灵Sprite的4种建立方式缓存
(1)根据图片资源路径来建立函数
?学习
1
2
3
4
|
//根据图片路径来建立
auto sprite1 = Sprite::create(filepath);
//根据图片路径来建立,并设置要显示的图片大小
auto sprite2 = Sprite::create(filepath,Rect(
0
,
0
,width,height));
|
(2)根据plist文件中的frame name建立,图片名称前要加#符号来区分字体
?动画
1
2
|
//参数:帧名字,frame name
auto sprite = Sprite::create(
'#ball.png'
);
|
(3)根据缓存plist中的sprite frame来建立,这种用的比较多spa
?code
1
2
3
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(
"loadingAndHP.plist"
,
"loadingAndHP.png"
);
//加载图片资源做为缓存
//从缓存图片中根据图片名字来建立
auto loading_bk=Sprite::createWithSpriteFrameName(
"loading_bk.png"
);
|
(4)根据纹理texture建立orm
1
2
3
4
5
6
|
//根据纹理图片来建立
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));
|
二、文字LabelTTF的建立方式
(1)根据字体、大小等多参数建立
1
2
3
4
5
6
|
auto center = LabelTTF::create(
"hello cocos2d-x"
,
//要显示字符串
"Paint Boy"
,
//字体
32
,
//字号
Size(s.width/
2
,
200
),
//显示的宽和高
TextHAlignment::CENTER,
//显示的位置,定位
TextVAlignment::TOP);
|
(2)根据自定义对象FontDefinition来建立
1
2
3
4
5
6
7
8
9
10
11
12
|
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.0中SpriteBatchNode,不鼓励使用,文档看这里点击打开连接,在此再也不赘述SpriteBatchNode的用法。
四、帧动画建立方式
经过多张图片文件来建立一个动画,
1
2
3
4
5
6
7
8
9
|
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对象建立的。
五、序列帧动画Sprite sheet animation
(1)经过.plist文件来建立
1
2
3
4
|
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(filename);
//加载.plist文件到缓存中
AnimationCache* cache = AnimationCache::getInstance()->addAnimationsWithFile(filename);
//根据动画.plist文件来建立动画缓存
Animation* animation = cache->animationByName(filename);
//直接从缓存中建立动画
Animate* animate = Animate::create(animation);
//生成动画动做
|
(2)经过数组来建立
1
2
3
4
5
6
7
8
9
|
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值得好好研究学习。