14. Genie advanced jobs 14. Genie advanced jobs
14. How to get started with genie
First, let us introduce our team's blog Optional Swift, and provide basic tutorials on swift, framework learning, uidesign, and control learning.
Optional swift team blog address
In the previous article, we provided a texture creation method. How can this problem be solved?Sprite KitFirst, you can find the image file (non-texture Gallery) by specifying the file name. If not, it will traverseapp bundleAll texture atlas in. This means that in a game project, you do not need to make changes to the Code to support this situation. After you design the image, you can just throw it to the texture gallery. It will automatically include the image.build.
Next we will discussSKTextureAtlasWhen you want to explicitly operate on a texture gallery, it is better to use it! First, create a texture image object (texture atlas object). Next, add the texture name to the texture gallery you just created. The following example is of practical development significance. The walking of a monster is through a texture Gallery (including multiple textures, which should be known by friends who have played gif games, in fact, gif is composed of multiple consecutive images .)Animate. In our actual project, we have addedmonster.atlasFolder, which contains four images. The implementation code is as follows:
// Create a texture gallery let atlas = SKTextureAtlas (named: "monster") // create a texture let f1 = atlas. textureNamed ("monster-walk1") let f2 = atlas. textureNamed ("monster-walk2") let f3 = atlas. textureNamed ("monster-walk3") let f4 = atlas. textureNamed ("monster-walk4") // create an array containing the f1 f2 f3 f4 belonging to the atlas of let monster1_textures = [f1, f2, f3, f4]
Note:
One frame is an image!
Texture creation method 2
First, let's talk about the texture, that is, reusable images.SKTextureIt's cool to create a texture! This section introduces another method, which is also a bit interesting. Since texture is an image, can we take a "Corner" of the image to create a new texture? The answer is yes, and the texture type created is stillSKTextureThe Code is as follows:
// The cornerTextures type is SKTexture let bottomLeftTexture = SKTexture (rect: CGRectMake (0.0, 0.0, 0.5, 0.5), inTexture: cornerTextures)
Let the genie get started!
Genie (Sprite(texture) Pointing to the current texture (that is, the current texture )! Monotonous? Change it! In this way, the texture attribute of the genie points to our favorite texture. The next scene is rendered with a new texture. However, we may need to set other attributes at any time to replace the New texture, for example:size,anchorPointAndcenterRect. Generally, we 'd better ensure that the image sizes in the texture Atlas are the same. I don't want to make any changes to the Character Images in the game ~
Let's let our monsters go! This is suddenly reminiscentActionWe need to know that our texture Atlas is actually some continuous images. When we create such an action, we can switch the picture in sequence. Isn't it playing the action?
You can play like this. Of course, if your friends and you have enough iron ....
And his video, and then crazy screenshots, come to a hundred pieces of images, open a storage folder, and then switch between them, isn't it an animation (unless he doesn't move ~)? This is just the same as ours. Next is the code.
// In this way, create an action and change the texture every 0.1 seconds. Of course, we pass in a texture array. How can we do it internally? let walkAnimation = SKAction. animateWithTextures (monster+textures, timePerFrame: 0.1) // let the genie start with monster. runAction (walkAnimation)
Go away after finishing the main event texture!
You must know that after the graphic hardware loads the texture to the memory, it is always savedSKTextureThe object is deleted. Therefore, we must consider a lot when deleting it, because all objects that have strong references to it will be affected by this (delete! Including:
- All references to the textureSKSpriteNodeAndSKEffectNodeObject
- This texture is strongly referenced in the Code (
strong references) Link
- SKTextureAtalsNeedless to say, the object does not include the reason for this texture.
End:
This section provides a better understanding of textures. I think it is very simple to understand the concept, so don't worry.
Next we will discuss the Action!