Create a sprite
You can use an image to create a sprite, PNG, JPEG, TIFF, WebP, these formats are all available, of course there are There are some other ways to create sprites, such as using atlas creation, and creating sprite caches.
auto mySprite = Sprite::create("mysprite.png");
The mysprite.png image is used directly above to create the sprite. The sprite will use the entire image, the resolution of the image, and the resolution of the sprite created. For example, the image is 200×200, and the sprite is also 200×200.
If you want to create a sprite whose size is only part of the original image, you can specify a rectangle when you create it, specify the four values of the rectangle, the initial x coordinate, the initial y coordinate, and the width of the rectangle. The height of the rectangle.
auto mySprite = Sprite::create("mysprite.png", Rect(0,0,40,40));
The initial coordinates of the rectangle, starting from the upper left corner of the figure , That is, the coordinates of the upper left corner is (0, 0), not from the lower left corner. So the resulting sprite is a small piece in the upper left corner of the image, starting from the upper left corner, the size is 40 x 40.
If you do not specify a rectangle, the Cocos2d-x engine will automatically use all the width and height of the image. See the example below. If you specify the width and height of the rectangle as the width and height of the image, the initial rectangle The coordinates are specified as (0, 0), then the effect is exactly the same as in the first case.
auto mySprite = Sprite::create("mysprite.png");
auto mySprite = Sprite::create("mysprite.png", Rect(0,0,200,200) );
Use atlas
Atlas (Sprite Sheet) is a resource that combines multiple images into one large image through a special tool, and is indexed through files in formats such as plist. Using atlas takes up less disk space than using multiple images. When using the atlas, first load all of them into SpriteFrameCache. SpriteFrameCache is a global cache class that caches the SpriteFrame objects added to it. SpriteFrame is only loaded once, and is kept in SpriteFrameCache all the time.
// 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");
Tools for creating atlas: Texture Packer, Zwoptex, ShoeBox, Sprite Sheet Packer.
Use sprite cache
The sprite cache is to improve the access speed of the sprite, and it provides a sprite cache mechanism.
// 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.< br />auto newspriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("Blue_Front1.png");
auto newSprite = Sprite::createWithSpriteFrame(newspriteFrame);
Control of sprites h3>
Anchor Point
The coordinate system used by the anchor point is based on the lower left solution as the origin (0,0). When you set the value of the anchor point, pay attention to at this point. By default, the anchor points of all node objects are (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
Sprite The position is affected by the anchor point,
// position a sprite to a specific position of x = 100, y = 200.
mySprite->setPosition(Vec2(100, 200)); pre>Rotate
Positive value rotates clockwise, negative value rotates counterclockwise.
/ 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);bloom
// 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);Tilt
// adjusts the X skew by 20.0
mySprite->setSkewX(20.0f);
< br />// adjusts the Y skew by 20.0
mySprite->setSkewY(20.0f);Color
// 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::WHITETransparency
// Set the opacity to 30, which makes this sprite 11.7% opaque.
// (30 divided by 256 equals 0.1171875...)
mySprite->setOpacity(30);Polygon Sprite
Ordinary sprites are divided into two triangles in the drawing process, and polygon sprites are divided into a series of triangles.
The reason for this is to improve performance, because in modern graphics processing, generally drawing fixed points consumes less performance than drawing pixels.
// Generate polygon info automatically.
auto pinfo = AutoPolygon::generatePolygon("filename.png");
// Create a sprite with polygon info.
auto sprite = Sprite::create(pinfo);
Create sprite
You can use an image to create Sprites, PNG, JPEG, TIFF, WebP, these formats are all available, of course, there are also some other ways to create sprites, such as using atlas to create them, or through sprite caches.
auto mySprite = Sprite::create("mysprite.png");
The mysprite.png image is used directly above to create the sprite. The sprite will use the entire image, the resolution of the image, and the resolution of the sprite created. For example, the image is 200x200, and the sprite is also 200x200.
If you want to create a sprite whose size is only part of the original image, you can specify a rectangle when you create it, specify the four values of the rectangle, the initial x coordinate, the initial y coordinate, and the width of the rectangle. The height of the rectangle.
auto mySprite = Sprite::create("mysprite.png", Rect(0,0,40,40));
The initial coordinates of the rectangle, starting from the upper left corner of the figure , That is, the coordinates of the upper left corner is (0, 0), not from the lower left corner. So the resulting sprite is a small piece in the upper left corner of the image, starting from the upper left corner, the size is 40 x 40.
If you do not specify a rectangle, the Cocos2d-x engine will automatically use all the width and height of the image. See the example below. If you specify the width and height of the rectangle as the width and height of the image, the initial rectangle The coordinates are specified as (0, 0), then the effect is exactly the same as in the first case.
auto mySprite = Sprite::create("mysprite.png");
auto mySprite = Sprite::create("mysprite.png", Rect(0,0,200,200) );
Use atlas
Atlas (Sprite Sheet) is a resource that combines multiple images into one large image through a special tool, and is indexed through files in formats such as plist. Using atlas takes up less disk space than using multiple images. When using the atlas, first load all of them into SpriteFrameCache. SpriteFrameCache is a global cache class that caches the SpriteFrame objects added to it. SpriteFrame is only loaded once, and is kept in SpriteFrameCache all the time.
// 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");
Tools for creating atlas: Texture Packer, Zwoptex, ShoeBox, Sprite Sheet Packer.
Use sprite cache
The sprite cache is to improve the access speed of the sprite, and it provides a sprite cache mechanism.
// 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.< br />auto newspriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("Blue_Front1.png");
auto newSprite = Sprite::createWithSpriteFrame(newspriteFrame);
Control of sprites h3>
Anchor point
The coordinate system used by the anchor point is based on the lower left solution as the origin (0,0). When you set the value of the anchor point, pay attention to at this point. By default, the anchor points of all node objects are (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
Sprite The position is affected by the anchor point,
// position a sprite to a specific position of x = 100, y = 200.
mySprite->setPosition(Vec2(100, 200)); pre>Rotate
Positive value rotates clockwise, negative value rotates counterclockwise.
/ 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);bloom
// 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);Tilt
// adjusts the X skew by 20.0
mySprite->setSkewX(20.0f);
< br />// adjusts the Y skew by 20.0
mySprite->setSkewY(20.0f);Color
// 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::WHITETransparency
// Set the opacity to 30, which makes this sprite 11.7% opaque.
// (30 divided by 256 equals 0.1171875...)
mySprite->setOpacity(30);< h3>Polygon Sprite
Ordinary sprites are divided into two triangles in the drawing process, and polygon sprites are divided into a series of triangles.
The reason for this is to improve performance, because in modern graphics processing, generally drawing fixed points consumes less performance than drawing pixels.
// Generate polygon info automatically.
auto pinfo = AutoPolygon::generatePolygon("filename.png");
// Create a sprite with polygon info.
auto sprite = Sprite::create(pinfo);