加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x精灵

发布时间:2020-12-14 17:28:39 所属栏目:百科 来源:网络整理
导读:精灵的创建 可以使用一张图像来创建精灵,PNG、JPEG、TIFF、WebP,这几个格式都可以,当然也有一些其它的方式可以创建精灵,如使用图集创建,通过精灵缓存创建。 auto mySprite = Sprite::create("mysprite.png"); 上面直接使用mysprite.png图像来创建精灵。

精灵的创建

可以使用一张图像来创建精灵,PNG、JPEG、TIFF、WebP,这几个格式都可以,当然也有一些其它的方式可以创建精灵,如使用图集创建,通过精灵缓存创建。

auto mySprite = Sprite::create("mysprite.png");

上面直接使用mysprite.png图像来创建精灵。精灵会使用整张图像,图像是多少的分辨率,创建出来的精灵就是多少的分辨率。比如图像是200x200,sprite也是200x200。

如果你想创建一个尺寸只有原始图像一部分的精灵,那你可以在创建的时候指定的个矩形,指定矩形的四个值,初始x坐标,初始y坐标,矩形宽,矩形的高。

auto mySprite = Sprite::create("mysprite.png",Rect(0,40,40));

矩形的初始坐标,从图形的左上角开始算,即左上角的坐标是 (0,0),不是从左下角。因此结果精灵是图像左上角的一小块,从左上角开始算起,40 x 40 的大小。

如果你没指定一个矩形,Cocos2d-x 引擎就会自动使用这个图像全部的宽和高,看下面的例子,如果你把矩形的宽高指定为图像的宽高,矩形的初始坐标指定为 (0,0),那这就和第一种情况的效果是完全一样的。

auto mySprite = Sprite::create("mysprite.png");

auto mySprite = Sprite::create("mysprite.png",200,200));

使用图集

图集(Sprite Sheet)是通过专门的工具将多张图片合并成一张大图,并通过plist等格式的文件索引的资源,使用图集比使用多个图像占用的磁盘空间更少。在使用图集时,首先将其全部加载到SpriteFrameCache中,SpriteFrameCache是一个全局的缓存类,缓存了添加到其的SpriteFrame对象。SpriteFrame只加载一次,后续一直保存在SpriteFrameCache中。

// 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");

创建图集工具:Texture Packer、Zwoptex、ShoeBox、Sprite Sheet Packer。

使用精灵缓存

精灵缓存是为了提高精灵的访问速度,提供的一个精灵的缓存机制。

// 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");
// 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);

精灵的控制

锚点

锚点使用的坐标系以左下解为原点(0,0),在你设置锚点的值时,要注意到这一点。默认情况下,所有的节点对象锚点是(0.5,0.5)。

// DEFAULT anchor point for all Sprites
mySprite->setAnchorPoint(0.5,0.5);

// bottom left
mySprite->setAnchorPoint(0,0);

// top left
mySprite->setAnchorPoint(0,1);

// bottom right
mySprite->setAnchorPoint(1,0);

// top right
mySprite->setAnchorPoint(1,1);

位置

精灵的位置受锚点影响,

// position a sprite to a specific position of x = 100,y = 200.
mySprite->setPosition(Vec2(100,200));

旋转

正值顺时针旋转,负值逆时针旋转。

/ rotate sprite by +20 degrees
mySprite->setRotation(20.0f);

// rotate sprite by -20 degrees
mySprite->setRotation(-20.0f);

// rotate sprite by +60 degrees
mySprite->setRotation(60.0f);

// rotate sprite by -60 degrees
mySprite->setRotation(-60.0f);

绽放

// increases X and Y size by 2.0 uniformly
mySprite->setScale(2.0);

// increases just X scale by 2.0
mySprite->setScaleX(2.0);

// increases just Y scale by 2.0
mySprite->setScaleY(2.0);

倾斜

// adjusts the X skew by 20.0
mySprite->setSkewX(20.0f);

// adjusts the Y skew by 20.0
mySprite->setSkewY(20.0f);

颜色

// 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)); // Same as Color3B::WHITE

透明度

// Set the opacity to 30,which makes this sprite 11.7% opaque.
// (30 divided by 256 equals 0.1171875...)
mySprite->setOpacity(30);

多边形精灵(Polygon Sprite)

普通精灵在绘图处理中被分为两个三角形,多边形精灵则是被分为一系列三角形。

这么做的原因是为了提高性能,因为在现代的图形处理中,一般绘制定点比绘制像素消耗的性能少。

// Generate polygon info automatically.
auto pinfo = AutoPolygon::generatePolygon("filename.png");

// Create a sprite with polygon info.
auto sprite = Sprite::create(pinfo);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读