Cocos2dx之Sprite
创建Sprite
所有游戏都有Sprite(精灵)对象。它是就是你在屏幕上移动的对象。游戏的主角也是一个Sprite(精灵)。并不是所有图形对象都是Sprite(精灵)对象。Sprite(精灵)对象是可以移动的,不能移动的就是一个Node(节点)。有很多创建Sprite(精灵)对象:我们可以通过格式为PNG, JPEG, TIFF的图片创建Sprite(精灵)。
1、通过指定的图片来创建Sprite
// This is how to create a sprite
auto mySprite = Sprite::create("mysprite.png");
上面的代码使用了整张mysprite.png图片来创建Sprite对象,图片有多大,Sprite对象就有多大,即如果图片文件是200 x 200,那么Sprite对象的大小就是200 x 200。
2、借助Rect对象来创建Sprite
上一步,创建的Sprite对象用了与原始图片文件一样的尺寸。借助Rect对象,我们可以只用原始图片的一部分来创建Sprite对象。
Rect对象有4个属性:origin x, origin y, width and height。Rect对象从左上角开始的,这刚好与屏幕的布局(从左下角开始)相反。在创建Sprite时,不指定Rect对象,那么Cocos2d-x就会自动使用图片的width和height。
auto mySprite = Sprite::create("mysprite.png", Rect(0,0,40,40));
3、通过Sprite Sheet创建Sprite
sprite sheet把多个Sprite包含在单个文件。sprite sheet可以帮助在成批调用draw 时获得更好的运行性能。 当使用sprite sheet时,它会被首先加载入SpriteFrameCache中,SpriteFrameCache是一个缓存类,它缓存着添加进来的SpriteFrame对象。 这样做是为了在未来可以更快速访问。SpriteFrame对象只会被加载一次,并缓存在SpriteFrameCache中。
sprite sheet的例子:
sprite sheet可以最大限度地减少不必要的空间和将所有sprites放在一个单独的文件中。
第一步:加载Sprite Sheet
加载sprite sheet到SpriteFrameCache,可以在AppDelegate完成:
// load the Sprite Sheet
auto spritecache = SpriteFrameCache::getInstance();// the .plist file can be generated with any of the tools mentioned below
spritecache->addSpriteFramesWithFile("sprites.plist");
注意:sprites.plist文件需要用特定的工具来生产。
第二步:通过SpriteFrameCache创建Sprite对象
在上一步中,我们已将sprite sheet加载到SpriteFrameCache中,所以我们可以使用它来创建Sprite对象。.plist
文件里,每个sprite都有一个名称与之对应:
// Our .plist file has names for each of the sprites in it. We'll grab
// the sprite named, "mysprite" from the sprite sheet:
auto mysprite = Sprite::createWithSpriteFrameName("mysprite.png");
4、通过SpriteFrame创建Sprite
通过从SpriteFrameCache提取SpriteFrame创建Sprite:
// this is equivalent to the previous example,
// but it is created by retrieving the SpriteFrame from the cache.
auto newspriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("Blue_Front1.png");
auto newSprite = Sprite::createWithSpriteFrame(newspriteFrame);
创建Sprite Sheets的工具
手动创建sprite sheet是一个很繁杂的过程。幸好,有以下这些工具帮助我们创建。
- Texture Packer (推荐)
- ShoeBox
- Zwoptex
- Sprite Sheet Packer
Sprites(精灵)还有一些可配置的属性,如:position(位置)、 rotation(旋转)、 scale(缩放)、 opacity(不透明度)、color(颜色)等等:
// this is how to change the properties of the sprite
mySprite->setPosition(Vec2(500, 0));mySprite->setRotation(40);// sets both the scale of the X and Y axis uniformly
mySprite->setScale(2.0); // Set the opacity to 30, which makes this sprite 11.7% opaque.
// (30 divided by 256 equals 0.1171875...)
mySprite->setOpacity(30);// set the color by passing in a pre-defined Color3B object.
mySprite->setColor(Color3B::WHITE);// Set the color by passing in a Color3B object.
mySprite->setColor(Color3B(255, 255, 255)); // Same as Color3B::WHITE
Polygon Sprite
Polygon Sprite(多边形精灵)也是一个Sprite。它是用来展示二维图片的,但同普通的Sprite不同。普通的Sprite对象是由两个三角形组成的矩形。而PolygonSprite对象则由一系列的三角形组成。
PolygonSprite是基于Sprite对象绘制,但就不是简单地围绕着的最大的宽和高的矩形来绘制。PolygonSprite会节省很多不必要的绘制。PolygonSprite对象也可以被用于spritesheets。Texture Packer这个工具多余的部分。PolygonSprite可以有效提高运行性能。
AutoPolygon
AutoPolygon是一个帮助类。它的用途是在运行时将图片转换成二维的多边形网格。最终创建出PolygonSprite对象:
// Generate polygon info automatically.
auto pinfo = AutoPolygon::generatePolygon("filename.png");// Create a sprite with polygon info.
auto sprite = Sprite::create(pinfo);
Anchor Point锚点
最后,所有Node(节点)对象都有一个anchor point锚点值。我们可以认为锚点作为一种以Sprite(精灵)的部分作为基本协调的方式:
// 左下角作为锚点
mySprite->setAnchorPoint(Vec2(0, 0));
// 左上角作为锚点
mySprite->setAnchorPoint(Vec2(0, 1));
// 右下角作为锚点
mySprite->setAnchorPoint(Vec2(1, 0));
// 右上角作为锚点
mySprite->setAnchorPoint(Vec2(1, 1));
// 中心作为锚点,默认
mySprite->setAnchorPoint(Vec2(0.5, 0.5));
这些将导致Sprite(精灵)的setPosition()调用以锚点为基础调整位置,默认是以Sprite(精灵)的的中心作为锚点。
Cocos2dx之Sprite相关推荐
- Cocos2d-x之Sprite
| 版权声明:本文为博主原创文章,未经博主允许不得转载. Sprite是Cocos2d-x游戏开发者最常用的类,用图片把精灵(Sprite)显示在屏幕上. 在游戏开发中,经常会遇到精灵(Sprit ...
- 【Cocos2d-x】Sprite精灵类-创建Sprite精灵对象
精灵在Cocos2d-x中的地位就像一场电影中的主角,男主角/女主角,是一场电影中的灵魂所在.也是我们游戏中的灵魂 无论我们控制的对象还是电脑控制的对象,我们都称之为精灵,或者背景中的图片,也可以称为 ...
- Cocos2d-x C++ Sprite Slice9渲染模式的应用
简介 在Slice9渲染模式中,会将需要渲染的纹理分为9块(不一定是均分,通过setCenterRect()设置中间区域的位置和大小,其他区域的位置和大小可以通过中间区域的矩形推算出来),在保持1.3 ...
- cocos2d-x,求世界坐标
老版: http://user.qzone.qq.com/350479720/blog/1384483239 一,求node的世界坐标. 因为node的contentSize为0,局部坐标原点与nod ...
- 【Cocos2D-x 3.5实战】坦克大战(2)游戏开始界面
关于游戏的素材都是在网上到处搜集到的,然后自己再用二流的ps技术修修改改的,所以有可能混在一起有点不搭调(没有办法啊,没有美工Orz.. 项目已经建立好了,然后我们需要把我们下载的素材放到Resour ...
- Cocos2d-x视频教程
目录 1. 我的技术专栏 2. 相关推荐 3. 下载链接 4. cocos2d-xx Lua+JS+C++教学视频 5. 杨丰盛Cocos2D-X游戏课程 6. [Cocos2d-x]塔防游戏开发实战 ...
- Cocos Creator JSB [Lv.3]
目录 摘要 资源 正式开始 为什么要使用`JSB` `JSB`能做什么 进行`JSB`绑定需要做什么 构建工程 创建类 绑定(搭建桥梁) 实现自定义的类型转换(非必须) 注册 将新增的文件加入`lib ...
- Cocos2d-导演、场景、层、精灵
Cocos2d-x-3.x引擎中,采用节点树形结构来管理游戏对象,一个游戏可以划分为不同的场景,每个场景可以划分为不同的层,每个层又可以拥有任意个可见的游戏节点(即对象,游戏中基本上所有的类都派生于节 ...
- 15、Cocos2dx 3.0游戏开发找小三之Sprite:每一个精灵都是上辈子折翼的天使
重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30475395 Sprite Sprite 能够说是 ...
最新文章
- 介绍下Nginx 反向代理与负载均衡
- 【新星计划】 Python的txt文本操作-读、写
- Python网络爬虫--BeautifulSoup库的基本元素
- shiro的登录 subject.login(token)中执行逻辑和流程
- express.static 和 lit-html
- Vivado FIFO IP核接口信号介绍
- 大家有没有发现,女生带上口罩后,感觉颜值升高了,尤其眼睛好看?
- 【poker】WASD变成上下左右键(方向键)的解决方法
- 终端启动tomcat报错 command not found 解决方法 (含启动和关闭命令)
- 没有做数据备份 网站随时毁于一旦
- Wicket实战(二)hello world
- css 实现一个尖角_css 实现一个带尖角的正方形
- AM437x——LED裸机
- 计算机图形学入门(十七)-光线追踪(蒙特卡洛积分与路径追踪)
- pip 生成 requirement.txt 文件
- 台式计算机cpu允许温度,台式cpu温度多少正常呢?老司机科普一下CPU常见问题
- 微信小程序:小程序开发中申请优惠券步骤
- 计算机 布局菜单栏 无法打勾,w7系统 我的电脑工具栏不见了
- 高数-导数的应用--函数凹凸性与拐点
- centos7搭建开源ERP-PSI