1
1
# Chapter 3: Sprites
2
2
3
3
## What are Sprites
4
- A Sprite is a 2D image that can be animated or transformed by changing the properties
5
- of it, including ` rotation ` , ` position ` , ` scale ` , ` color ` , etc.
4
+ A Sprite is a 2D image that can be animated or transformed by changing its properties,
5
+ including ` rotation ` , ` position ` , ` scale ` , ` color ` , etc.
6
6
7
7
## Creating Sprites
8
- There are different ways to create Sprites depending upon what you need to accomplish. You
9
- can create a ` Sprite ` from a number of graphic formats including: PNG, JPEG, TIFF. PVRv2,
10
- PVRv2, ETC1 and others. Let's go through some create methods and talk about each one.
8
+ There are different ways to create Sprites depending upon what you need to accomplish. You
9
+ can create a ` Sprite ` from a number of graphic formats including: PNG, JPEG, TIFF, and others.
10
+ Let's go through some create methods and talk about each one.
11
11
12
12
### Creating a Sprite
13
- A ` Sprite ` can be created by specifying a specific image file to use.
13
+ A ` Sprite ` can be created by specifying an image file to use.
14
14
15
15
``` cpp
16
16
auto mySprite = Sprite::create(" mysprite.png" );
17
17
```
18
18
19
19
![ ] ( 3/i1.png " ")
20
- This creates a ` Sprite ` from 'mysprite.png'. The result is a ` Sprite ` of the graphic you
21
- loaded. It has the same width and height. If the file is 200x200 the resulting ` Sprite ` is
22
- the same size.
23
20
24
- ### Creating a Sprite From a Rect
25
- This creates a ` Sprite ` by specifying a file name and a Rect to use as the selection of
26
- what the sprites representation is. ` setRect() ` is the only way to resize a ` Sprite `
21
+ This creates a ` Sprite ` using the 'mysprite.png' image. The result is that the
22
+ created ` Sprite ` uses the whole image. ` Sprite ` has the same dimensions
23
+ of ` mysprite.png ` . If the image file is 200x200 the resulting ` Sprite ` is 200x200.
24
+
25
+ ### Creating a Sprite with a Rect
26
+
27
+ In the previous example, the created ` Sprite ` has the same size as the original PNG.
28
+ If you want to create a ` Sprite ` with only a certain portion of the image file, you can
29
+ do it by specifying a ` Rect ` .
30
+
31
+ ` Rect ` has 4 values: origin x, origin y, width and height.
32
+
27
33
``` cpp
28
34
auto mySprite = Sprite::create(" mysprite.png" , Rect (0 ,0 ,40 ,40 ));
29
35
```
36
+
30
37
![ ] ( 3/i4.png " ")
31
38
39
+ FIXME: Sprites in screenshot must have same position
40
+
41
+ FIXME: Explain that ` Rect ` uses the top-left origin
42
+
43
+ The result is that we are creating a ` Sprite ` with only a portion of the PNG.
44
+ In this case, the ` Sprite ` size will be 40x40.
45
+
46
+ FIXME: Say that these 2 examples are equivalents for this particular case
47
+
48
+ ``` cpp
49
+ auto mySprite = Sprite::create(" mysprite.png" );
50
+ auto mySprite = Sprite::create(" mysprite.png" , Rect (0 ,0 ,200 ,200 ));
51
+ ```
52
+
53
+
32
54
## Creating a Sprite from a Sprite Sheet
33
- A ` sprite sheet ` a way to combine sprites into a single texture while reducing
34
- the overall size compared to loading each ` Sprite ` individually. Why does this
35
- matter? When loading a ` Sprite ` each hardware platform has constraints of a
36
- minimum texture size that it will need to load. As you can imagine there is
37
- a lot of unused space and this increases memory consumption, draw times and
38
- lowers your games frame rate.
39
-
40
- Using a ` sprite sheet ` allows all of your ` Sprite ` objects to be condensed filing
41
- unused space. Now only the entire spritesheet must meet the hardwares constraints
42
- and can be loaded all at the same time. This means you will significantly reduce
43
- memory usage, reduce the time it takes OpenGL to draw your sprites and keep the
44
- frame rate of your game high. Behind the scenes a sprite sheet is a combination
45
- of 2 files. The first is a file that describes each ` Sprite ` in the sprite sheet
46
- and its size in ` Rect ` format. The second is the actual graphic images compacted
47
- together fo create as small a file as possible.
48
-
49
- When using a ` sprite sheet ` , it is first loaded, in it's entirety, into the
50
- ` SpriteFrameCache ` . ` SpriteFrameCache ` is a caching class that retains Sprites
51
- quicker future access. This prevents needing to load a ` Sprite ` multiple times,
52
- over and over. The ` Sprite ` is loaded once and reatined in the ` SpriteCache `
55
+ A _ sprite sheet_ is a way to combine sprites into a single file. This reduces the
56
+ overall file size compared to having individual files for each ` Sprite ` . This means
57
+ you will significantly reduce memory usage, file size and loading time.
58
+
59
+ Also, using sprite sheets is a needed condition in order to achive better performance
60
+ by _ batching_ the _ draw calls_ . More on this in the Advanced Chapter.
61
+
62
+ When using a ` sprite sheet ` , it is first loaded, in its entirety, into the
63
+ ` SpriteFrameCache ` . ` SpriteFrameCache ` is a caching class that retains ` SpriteFrame ` for
64
+ future quicker access.
65
+
66
+ And a ` SpriteFrame ` is a object that contains the image file name and a ` Rect ` that
67
+ specifies the size of the sprite.
68
+
69
+ The ` SpriteFrameCache ` prevents needing to load a ` SpriteFrame ` multiple times,
70
+ over and over. The ` SpriteFrame ` is loaded once and retained in the ` SpriteFrameCache `
53
71
54
72
Here is an example sprite sheet:
55
73
74
+ FIXME: Low priority. Show a sprite sheet that maximes the space.
75
+
76
+ FIXME: Horizontal spritesheet is better because it takes less space
77
+
56
78
![ ] ( 3/3_1.png " example SpriteSheet ")
57
79
58
80
Let's tie this all together!
@@ -62,30 +84,40 @@ Load your `sprite sheet` into the `SpriteFrameCache`, probably in `AppDelegate`:
62
84
``` cpp
63
85
// load the Sprite Sheet
64
86
auto spritecache = SpriteFrameCache::getInstance();
87
+
88
+ // the .pist file can be generated with any of the tools mentioned below
65
89
spritecache->addSpriteFramesWithFile ("sprites.plist");
66
90
```
67
- Now that we have a `sprite sheet` loaded into `SpriteFrameCache` we can create `Sprite` objects
91
+ Now that we have a `sprite sheet` loaded into `SpriteFrameCache` we can create `Sprite` objects
68
92
by utilizing it.
69
93
70
94
### Creating a Sprite from SpriteFrameCache
71
- This creates a `Sprite` by pulling it from the `SpriteFrameCache`.
95
+ This creates a `Sprite` by pulling it from the `SpriteFrameCache`.
72
96
```cpp
73
97
auto mysprite = Sprite::createWithSpriteFrameName("mysprite.png");
74
98
```
99
+
75
100
![ ] ( 3/i3.png " ")
76
101
77
102
### Creating a Sprite from a SpriteFrame
78
- A ` Sprite ` can be created from a ` SpriteFrame ` . A ` SpriteFrame ` is a specified texture
79
- and defined by a specific size ` Rect `
103
+
104
+ Another way to create the same ` Sprite ` is by fetching the ` SpriteFrame ` from the
105
+ ` SpriteFrameCache ` , and then creating the ` Sprite ` with the ` SpriteFrame ` .
106
+ Example:
107
+
80
108
``` cpp
81
- auto newspriteFrame = SpriteFrame::create(" Blue_Front1.png" , Rect (0 ,0 ,50 ,50 ));
82
-
109
+ // this is equivalent to the previous example,
110
+ // but you need to type more
111
+ auto newspriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName ("mysprite.png");
83
112
auto newSprite = Sprite::createWithSpriteFrame(newspriteFrame);
84
113
```
85
114

86
115
87
116
### Tools for creating Sprite Sheets
88
117
118
+ FIXME: add some text, like... creating spritesheet manually is a tedious work, fortunately
119
+ there are tools that generates them automatically.
120
+
89
121
[Cocos Studio](http://www.cocos2d-x.org/wiki/CocoStudio)
90
122
91
123
[Texture Packer](https://www.codeandweb.com/texturepacker)
@@ -101,8 +133,14 @@ auto mySprite = Sprite::create("mysprite.png");
101
133
```
102
134
![ ] ( 3/i1.png " ")
103
135
136
+ FIXME: describe color and opacity then describe the properties first: position, rotation, scale, skew. And then
137
+ describe in more detail what Anchor point is, the coordinate system is uses, etc.
138
+
104
139
### Scale
105
140
Changes the * Sprites* scale, either by x, y or uniformly for both x and y.
141
+
142
+ FIXME: A scale of 2 will take less space for the screenshot
143
+
106
144
``` cpp
107
145
// increases X and Y size by 4.0 uniformly
108
146
mySprite->setScale (4.0);
@@ -116,7 +154,7 @@ mySprite->setScaleY(4.0);
116
154

117
155
118
156
### Anchor Point and Position
119
- `Anchor Point` is a point that you set as a way of specifying what part of
157
+ `Anchor Point` is a point that you set as a way of specifying what part of
120
158
the `Sprite` will be used when setting the position of it.
121
159
```cpp
122
160
// bottom left
0 commit comments