零基础学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");
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaWFteW9jb2Nv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
最后一个因为没有缓存就无法实现了。要用工具生成plist文件啥的。
最终写完了,尽管好短暂,可是还是好理解的。尤其后面那两个函数,看了非常久啊~~~~~苦涩啊~~~~~
预告下一篇是讲精灵经常使用的函数。
。
。
。
。。
零基础学Cocos2d-X 3.0 - 04的更多相关文章
- 【雕爷学编程】MicroPython动手做(04)——零基础学MaixPy之尝试运行
1.hello micropython #MicroPython动手做(04)——零基础学MaixPy之基本示例 #程序之一:hello micropython #MicroPython动手做(04) ...
- 《Windows编程零基础学》第零节
首先很开心申请到了这一个专栏<Windows编程零基础学> 这是第一篇文章,在这里,我将讲述一些基础的知识. 什么是Windows编程 所谓Windows编程就是在Windows平台上开发 ...
- [Python] 文科生零基础学编程系列二——数据类型、变量、常量的基础概念
上一篇:[Python] 文科生零基础学编程系列--对象.集合.属性.方法的基本定义 下一篇: (仍先以最简单的Excel的VBA为例,语法与Python不同,但概念和逻辑需要理解透彻) p.p1 { ...
- [Python] 文科生零基础学编程系列三——数据运算符的基本类别
上一篇:[Python] 文科生零基础学编程系列二--数据类型.变量.常量的基础概念 下一篇: ※ 程序的执行过程,就是对数据进行运算的过程. 不同的数据类型,可以进行不同的运算, 按照数据运算类型的 ...
- 零基础学python-7.2 字符串常量
1.单双引號字符串是一样的 >>> 'abc',"abc" ('abc', 'abc') >>> 当你的python照着上面的样例来写,这个时候 ...
- 《零基础学JavaScript(全彩版)》学习笔记
<零基础学JavaScript(全彩版)>学习笔记 二〇一九年二月九日星期六0时9分 前期: 刚刚学完<零基础学HTML5+CSS3(全彩版)>,准备开始学习JavaScrip ...
- 《零基础学HTML5+CSS3(全彩版)》读书笔记
2019年1月31日星期四 1点 <零基础学HTML5+CSS3(全彩版)>开始全面学习 前提: 11月20日开始学Python,可能因为太累了,也可能遇到了瓶颈,进入了一个迷茫期,1月6 ...
- 零基础学python-7.7 字符串格式化方法(1)
承接上一章节.我们这一节来说说字符串格式化的还有一种方法.就是调用format() >>> template='{0},{1} and {2}' >>> templ ...
- 零基础学python-6.2 共享引用
这一章节说说共享引用 我们先举一个样例 a=1 b=a 上面的样例就是共享引用,这里我们说说整个过程: 1.创建一个对象1 2.创建一个变量a 3.把a和1所在的内存空间连接起来.就是a引用1 4.a ...
- 零基础学Python_汇总贴
https://time.geekbang.org/course/intro/98 零基础学Python-第一章 :Python介绍和安装-01.Python语言的特点 零基础学Python-第一章 ...
随机推荐
- flask jinja的宏
form中关于表单的定义 class AreaListForm(Form): area1 = BooleanField(u'1区', default=False) area2 = BooleanFie ...
- Java Int和Integer有什么区别?
Int int是我们常说的整型数字,是Java的8个原始数据类型(Primitive Type:boolean.byte.short.char.int.float.double.long)之一.Jav ...
- 20145221 《Java程序设计》第六周学习总结
20145221 <Java程序设计>第六周学习总结 教材学习内容总结 第十一章部分 - 输入与输出 文件的读写 网络上传数据的基础 同样要先掌握父类中方法,核心类如下: 以上则是老师提出 ...
- xss总结(一直更新)
反射型: 在表单输入jack网页源代码:<pre>Hello jack</pre> 测试: 低级别:<script>alert('xss')</script& ...
- Python学习札记(三十二) 面向对象编程 Object Oriented Program 3
参考:访问限制 NOTE 1.eg. #!/usr/bin/env python3 class Student(object): """docstring for Stu ...
- scrapy中的canonicalize_url【转】
转自:http://www.leyle.com/archives/canonicalize_url.html 思考一下:对url进行规范化处理是否是必须的?因为这一步处理涉及到编码转换,对于一个网页的 ...
- fiddler抓包https
http://blog.csdn.net/idlear/article/details/50999490 charles也能抓取https请求 http://blog.csdn.net/jiadoud ...
- 学习gulpfile.babel.js随笔
'use strict' import gulp from 'gulp' //将gulp插件包含进来 import sass from 'gulp-sass' //编译sass文件 import im ...
- Sum Problem
2018-04-22 19:59:52 Sum系列的问题是Leetcode上的一个很经典的系列题,这里做一个简单的总结. 167. Two Sum II - Input array is sorted ...
- Java 集合-Map接口和三个子类实现
2017-10-31 22:05:59 Map 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. HashMap是基于散列表实现的,插入.删除和定位元素时间复杂度平均能达到O ...