
可以使用一张图像来创建精灵,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 Sheetauto spritecache = SpriteFrameCache::getInstance();// the .pList file can be generated with any of the tools mentioned belowspritecache->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 SpritesmySprite->setAnchorPoint(0.5,0.5);// bottom leftmySprite->setAnchorPoint(0,0);// top leftmySprite->setAnchorPoint(0,1);// bottom rightmySprite->setAnchorPoint(1,0);// top rightmySprite->setAnchorPoint(1,1);位置
精灵的位置受锚点影响,
// position a sprite to a specific position of x = 100,y = 200.mySprite->setposition(Vec2(100,200));旋转
正值顺时针旋转,负值逆时针旋转。
/ rotate sprite by +20 degreesmySprite->setRotation(20.0f);// rotate sprite by -20 degreesmySprite->setRotation(-20.0f);// rotate sprite by +60 degreesmySprite->setRotation(60.0f);// rotate sprite by -60 degreesmySprite->setRotation(-60.0f);绽放
// increases X and Y size by 2.0 uniformlymySprite->setScale(2.0);// increases just X scale by 2.0mySprite->setScaleX(2.0);// increases just Y scale by 2.0mySprite->setScaleY(2.0);倾斜
// adjusts the X skew by 20.0mySprite->setSkewX(20.0f);// adjusts the Y skew by 20.0mySprite->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); 总结 以上是内存溢出为你收集整理的cocos2d-x精灵全部内容,希望文章能够帮你解决cocos2d-x精灵所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)