Cocos2d-x3.2 Sprite精灵类的创建与设置

前端之家收集整理的这篇文章主要介绍了Cocos2d-x3.2 Sprite精灵类的创建与设置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//3.0版本以后,sharedXXX方法改成了getInstance方法 //Director::getInstance()->getVisibleSize()

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//GameScene.h
#include"cocos2d.h"
class GameScene: public cocos2d::Layer
{
:
static cocos2d::Scene*createScene();
virtual bool init();
void menuCallback(cocos2d::Ref*pSender);
CREATE_FUNC(GameScene);
};


?
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//GameScene.cpp
#include"GameScene.h"
USING_NS_CC;
cocos2d::Scene*GameScene::createScene()
{
auto scene=Scene::create(); //创建一个场景
layer=GameScene::create(); //创建一个图层
scene->addChild(layer);
return scene;
}
//初始化当前的图层
GameScene::init()
{
if (!Layer::init()) //初始化父类
return false ;
//获取屏幕大小
SizevisibleSize=Director::getInstance()->getVisibleSize();
//创建精灵的六种方法
//方法
sprite1=Sprite::create( "Flag_1.png" );
sprite1->setPosition(Vec2(visibleSize.width*0.2,visibleSize.height*0.7));
this ->addChild(sprite1);
//方法
sprite2=Sprite::create( ,Rect(0,48,36));
sprite2->setPosition(Vec2(visibleSize.width*0.4,visibleSize.height*0.7));
->addChild(sprite2);
//创建2D纹理
texture1=Director::getInstance()->getTextureCache()->addImage( );
//autotexture1=TextureCache::sharedTextureCache()->addImage("Flag_1.png");//cocos2d-x3.0已移除该方法
//方法
sprite3=Sprite::createWithTexture(texture1);
sprite3->setPosition(Vec2(visibleSize.width*0.6,visibleSize.height*0.7));
->addChild(sprite3);
//方法
sprite4=Sprite::createWithTexture(texture1,36));
sprite4->setPosition(Vec2(visibleSize.width*0.8,visibleSize.height*0.7));
->addChild(sprite4);
@H_301_503@
//创建精灵帧
frame=SpriteFrame::create(
//方法
sprite5=Sprite::createWithSpriteFrame(frame);
sprite5->setPosition(Vec2(visibleSize.width*0.3,visibleSize.height*0.3));
->addChild(sprite5);
////方法
//SpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("img.plist");
//
//autosprite6=Sprite::createWithSpriteFrameName("HelloWorld.png");
//sprite6->setPosition(Vec2(visibleSize.width*0.7,visibleSize.height*0.3));
//this->addChild(sprite6);
//**************************//
//精灵常用处理操作
//设置缩放
sprite1->setScale(1.5f,1.5f);
//sprite1->setScaleX(1.5f);
//sprite1->setScaleY(1.5f);
//设置旋转,大于零顺时针旋转
sprite2->setRotation(60.0f);
//sprite2->setRotationX(60.0f);
//sprite2->setRotationY(60.0f);
//设置倾斜
sprite3->setSkewX(30.0f);
//sprite3->setSkewY(30.0f);
//设置是否可见
sprite4->setVisible( true );
//设置颜色
sprite1->setColor(Color3B(0,255));
//设置透明度
sprite2->setOpacity(100);
//更改图片
;
}



FROM:http://my.oschina.net/Jacedy/blog/301242

原文链接:https://www.f2er.com/cocos2dx/340260.html

猜你在找的Cocos2d-x相关文章