零基础学Cocos2d-X 3.0 - 04

时间:2023-03-10 03:13:04
零基础学Cocos2d-X 3.0 - 04

忙完两个项目后。最终有时间继续学习Cocos2d-X 了。

常听人说。Cocos2d-X 有四个类是最经常使用的,包含:

Director 类----> 导演

Scene 类 -----> 场景

Layer 类 ------> 层

Sprite 类 ------> 精灵

略微大概理解一下吧。

一个舞台仅仅有一个导演在执导,所以这个是比較好理解的。

一个舞台会上演非常多场场景戏,不同的场景会在导演的指示下进行切换、进行,这个也没什么问题。

一个场景能够放非常多层,就正如你在开发iOS的时候,你会给一个self.view 增加非常多 subview的道理一样,这个意会吧,我还不能言传。

一个舞台上有非常多工作人员。他们能够是百般武艺,能够是变化无穷。太难形容了。直接叫工作人员做精灵好了。

从之前的代码能够看出,

导演 Director 类是一个单例

使用方式,如获取屏幕大小:

Director *pDirector = Director::getInstance();
Size visibleSize = pDirector->getVisibleSize();

或者是

Size visibleSize = Director::getInstance()->getVisibleSize();

好方便的啊~~~~~不愧是单例。

场景 Scene 类,在AppDelegate.cpp 里面,我们也见过,有时候想想。是不是和ViewController非常像啊

Scene *scene = HelloWorld::createScene();
director->runWithScene(scene);

相似,我仅仅是说相似而已 : )

ViewController *viewController = [[ViewController alloc] init];
self.window.rootViewController = viewController;

一个舞台能够由一个Scene 或多个Scene 组成,就好像iOS 应用能够由一个ViewCotnroller 或多个ViewController组成那样。

层 Layer 类,我感觉用iOS 的视图形容它也应该能够的吧。

你看一个Scene 能够由一个Layer 或多个Layer 组成,而ViewController 也能够由一个 View 或多个 View 组成。多像啊。

Layer 是非常重要的。通常多个Layer去实现一个应用、游戏。

精灵 Sprite 类。

就是游戏中一个对象。角色。

关于它的我们在稍后将会讲讲。

精灵 Sprite 的创建

    /// @{
/// @name Creators /**
* Creates an empty sprite without texture. You can call setTexture method subsequently.
*
* @return An autoreleased sprite object.
*/
static Sprite* create(); /**
* Creates a sprite with an image filename.
*
* After creation, the rect of sprite will be the size of the image,
* and the offset will be (0,0).
*
* @param filename A path to image file, e.g., "scene1/monster.png"
* @return An autoreleased sprite object.
*/
static Sprite* create(const std::string& filename); /**
* Creates a sprite with an image filename and a rect.
*
* @param filename A path to image file, e.g., "scene1/monster.png"
* @param rect A subrect of the image file
* @return An autoreleased sprite object
*/
static Sprite* create(const std::string& filename, const Rect& rect); /**
* Creates a sprite with a Texture2D object.
*
* After creation, the rect will be the size of the texture, and the offset will be (0,0).
*
* @param texture A pointer to a Texture2D object.
* @return An autoreleased sprite object
*/
static Sprite* createWithTexture(Texture2D *texture); /**
* Creates a sprite with a texture and a rect.
*
* After creation, the offset will be (0,0).
*
* @param texture A pointer to an existing Texture2D object.
* You can use a Texture2D object for many sprites.
* @param rect Only the contents inside the rect of this texture will be applied for this sprite.
* @param rotated Whether or not the rect is rotated
* @return An autoreleased sprite object
*/
static Sprite* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated=false); /**
* Creates a sprite with an sprite frame.
*
* @param spriteFrame A sprite frame which involves a texture and a rect
* @return An autoreleased sprite object
*/
static Sprite* createWithSpriteFrame(SpriteFrame *spriteFrame); /**
* Creates a sprite with an sprite frame name.
*
* A SpriteFrame will be fetched from the SpriteFrameCache by spriteFrameName param.
* If the SpriteFrame doesn't exist it will raise an exception.
*
* @param spriteFrameName A null terminated string which indicates the sprite frame name.
* @return An autoreleased sprite object
*/
static Sprite* createWithSpriteFrameName(const std::string& spriteFrameName); /// @} end of creators group

从精灵 Sprite 类的头文件能够看到有哪些方法能够创建精灵。

/**********************切割线************************/

创建一个没有贴图的精灵

函数:

static Sprite* create();

使用:

    //创建空白的精灵
Sprite *aSprite = Sprite::create();
//为精灵加上贴图
aSprite->setTexture("Icon-50.png");
//为精灵设置坐标
aSprite->setPosition(30, 160);
//将精灵增加当前层
this->addChild(aSprite);

/**********************切割线************************/

通过图像名创建一个精灵

函数:

static Sprite* create(const std::string& filename);

使用:

    //通过图像名创建精灵
Sprite *bSprite = Sprite::create("Icon-50.png");
//为精灵设置坐标
bSprite->setPosition(95, 160);
//将精灵增加当前层
this->addChild(bSprite);

/**********************切割线************************/
通过图像名和指定切割该图像的范围创建精灵

函数:

static Sprite* create(const std::string& filename, const Rect& rect);

使用:

    //通过图像名和指定切割该图像的范围创建精灵
Sprite *cSprite = Sprite::create("Icon-50.png", Rect(0, 0, 30, 30));
//为精灵设置坐标
cSprite->setPosition(150, 160);
//将精灵增加当前层
this->addChild(cSprite);

/**********************切割线************************/

通过Texture2D 对象创建精灵

函数:

static Sprite* createWithTexture(Texture2D *texture);

使用:

    //首先创建 Image对象
Image *image = new Image();
//为 Image对象 设置图像。通过指定路径
image->initWithImageFile("Icon-50.png");
//创建 Texture2D对象
Texture2D *text = new Texture2D();
//为 Texture2D对象 增加 图像内容
text->initWithImage(image);
//通过贴图创建精灵
Sprite *dSprite = Sprite::createWithTexture(text);
//为精灵设置坐标
dSprite->setPosition(215, 160);
//将精灵增加当前层
this->addChild(dSprite);

/**********************切割线************************/
通过Texture2D 对象和指定切割该对象的范围创建精灵
函数:

static Sprite* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated=false);

使用:

    //首先创建 Image对象
Image *image2 = new Image();
//为 Image对象 设置图像,通过指定路径
image2->initWithImageFile("Icon-50.png");
//创建 Texture2D对象
Texture2D *text2 = new Texture2D();
//为 Texture2D对象 增加 图像内容
text2->initWithImage(image);
//通过贴图和切割该贴图创建精灵
Sprite *eSprite = Sprite::createWithTexture(text2, Rect(0, 0, 30, 30));
//为精灵设置坐标
eSprite->setPosition(280, 160);
//将精灵增加当前层
this->addChild(eSprite);

/**********************切割线************************/

利用还有一帧创建一个精灵

函数:

static Sprite* createWithSpriteFrame(SpriteFrame *spriteFrame);

使用:

    //SpriteFrame对象。通过指定图像和切割该图像的參数创建
SpriteFrame *frame = SpriteFrame::create("Icon-50.png", Rect(0, 0, 40, 30));
//通过 SpriteFrame对象 创建精灵
Sprite *fSprite = Sprite::createWithSpriteFrame(frame);
//为精灵设置坐标
fSprite->setPosition(345, 160);
//将精灵增加当前层
this->addChild(fSprite);

/**********************切割线************************/

利用帧缓存中一帧的名字创建一个精灵

函数:

static Sprite* createWithSpriteFrameName(const std::string& spriteFrameName);

使用:

    //通过 SpriteFrameName对象 创建精灵
Sprite *gSprite = Sprite::createWithSpriteFrameName("Icon-50.png");
//为精灵设置坐标
gSprite->setPosition(410, 160);
//将精灵增加当前层
this->addChild(gSprite);

    //实现的源代码分析
//用 SpriteFrameName 的參数从 SpriteFrameCache 中获取 SpriteFrame对象
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("testIcon.plist");

零基础学Cocos2d-X 3.0 - 04

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaWFteW9jb2Nv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

最后一个因为没有缓存就无法实现了。要用工具生成plist文件啥的。

最终写完了,尽管好短暂,可是还是好理解的。尤其后面那两个函数,看了非常久啊~~~~~苦涩啊~~~~~

预告下一篇是讲精灵经常使用的函数。

。。