cocos2d-x精灵

精灵的创建

可以使用一张图像来创建精灵,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,0,40,40));

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

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

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

auto mySprite = Sprite::create("mysprite.png", Rect(0,0,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。

使用精灵缓存

精灵缓存是为了提高精灵的访问速度,提供的一个精灵的缓存机制。我们可以创建一个精灵并把精灵放在SpriteFrameCache中。也可以从SpriteFrameCache访问一个精灵。

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

 

最后欢迎大家访问我的个人网站:1024s​​​​​​​

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值