- iOS Game Programming Cookbook
- Bhanu Birani Chhavi Vaishnav
- 885字
- 2021-07-23 20:03:20
Applying actions on sprites
Sprites are just static images with no life. So actions add that life to the sprites that make your game. Actions help in building the gameplay by moving sprites and animating them differently. An action is an object that makes the scene look alive.
Actions are applied on nodes and sprites, for example, we want to move some object that is a sprite, so we create a move action and run it on that sprite. SpriteKit automatically changes the sprite's position in a form of animation until the action is over.
All actions are implemented using a class called SKAction
and different types of actions are instantiated using the class methods of the SKAction
class provided for various animation functionality.
Here are the most common actions available in SpriteKit:
- Applying transformation (translation, rotation, and scaling)
- Changing visibility (fading in and fading out)
- Changing the content of the sprite
- Changing the colors of sprites
- Removing sprites
- Calling a block or a selector
- Repeating and sequencing actions
Getting ready
To apply different actions on sprites and see them animating, we need to know scenes, sprites, and the overall life cycle of a SpriteKit project. We also need to know about some fundamental actions that are applied on any entity such as move, rotate, scale, and there are many more special effects to explore.
How to do it...
There is a wide variety of actions to be applied on nodes and sprites, some of which are listed next.
To understand this, we will take the spaceship as a sprite to apply different actions.
There are several individual actions provided by the SpriteKit framework. A few of them are explained as follows:
- Move Action: To move a sprite, call the class method shown below, specifying the location where the sprite has to be moved and in what time. And then call the
runAction
method on the sprite with the move action created.SKAction* moveAction = [SKAction moveTo:CGPointMake(100,100) duration:1.0]; [self.spaceShipSprite runAction:moveAction];
- Rotate Action: To rotate a sprite, we have to specify an angle in radians, which will make the sprite rotate by or to that angle in a specified time. So specify the angle in degrees, convert to radians, and then feed it to the function thereby applying that action to the sprite.
CGFloat angleInDegree = 90.0; CGFloat angleInRadian = angleInDegree * M_PI/180.0; SKAction* rotateAction = [SKAction rotateByAngle:angleInRadian duration:2.0]; [self.spaceShipSprite runAction:rotateAction];
- Scale Action: To scale a sprite, we have to specify a scale factor, which will increase or decrease the size of the sprite, depending on the scale factor given in a time.
SKAction* scaleAction = [SKAction scaleBy:2.0 duration:2.0]; [self.spaceShipSprite runAction:scaleAction];
- Fade Action: To make a sprite visible or invisible through animation, there are methods for fading out and fading in a sprite. For now, fading out is shown in the following code, which takes a parameter or the time over which to fadeout.
SKAction* fadeOutAction = [SKAction fadeOutWithDuration:1.0]; [self.spaceShipSprite runAction:fadeOutAction];
In SpriteKit, there are many more actions for giving delays, changing content, invoking an object or selector, calling blocks, and many special effects.
Similar to the individual actions, there are sequence and repeat actions, which fall under a different category of actions provided by SpriteKit. The sequence action is meant for running actions in a particular sequence we want. As shown in the following code, two actions are created—one for fading out and the other for fading in the sprite. So, both the actions are fed to the sequence action in the order we want and it would run the sequence we asked for:
SKAction* fadeOutAction = [SKAction fadeOutWithDuration:1.0]; SKAction* fadeInAction = [SKAction fadeInWithDuration:1.0]; SKAction* sequenceAction = [SKAction sequence:@[fadeOutAction, fadeInAction]]; [self.spaceShipSprite runAction:sequenceAction];
The repeat action allows actions to be repeated for a fixed number of time or to be repeated forever. So using the preceding sequence action, we do both.
- Animating the sequence for three times regularly:
SKAction* repeatThreeTimesAction = [SKAction repeatAction:sequenceAction count:3]; [self.spaceShipSprite runAction:repeatThreeTimesAction];
- Animating the sequence repeatedly forever:
SKAction* repeatForeverAction = [SKAction repeatActionForever:sequenceAction]; [self.spaceShipSprite runAction:repeatForeverAction];
Another type of action is the group action. Several times in games we may need to repeat a sequence of actions, which means running actions in a particular sequence for any interval of time. As shown previously, two actions were created, one for fading out and second for fading in the sprite. So, both the actions were fed to the sequence action in the order we wanted and it would run the sequence we asked for.
Group actions are used when we have to run many actions at the same point of time. So we can make a group function that moves a sprite by fading out also:
SKAction* moveAction = [SKAction moveTo:CGPointMake(100,100) duration:1.0]; SKAction* fadeOutAction = [SKAction fadeOutWithDuration:1.0]; SKAction *groupAction = [SKAction group:@[moveAction, fadeOutAction]]; [self.spaceShipSprite runAction:groupAction];
How it works...
All the actions we have discussed earlier will be working in the same flow. Here is the basic anatomy of all actions, which we apply on sprites:
- Decide the actions we want to apply, in what sequence they are to be executed, and whether some action needs to be repeated or not.
- For each action, whatever the type is, specify its respective parameters and the time duration for it.
- After finalizing the action, just call
runAction
on the sprite to be animated with the action constructed.
- Mastering NetBeans
- Implementing Modern DevOps
- Spring 5.0 By Example
- R語言游戲數據分析與挖掘
- Django:Web Development with Python
- 精通Scrapy網絡爬蟲
- Elasticsearch for Hadoop
- 青少年Python編程入門
- R Data Analysis Cookbook(Second Edition)
- Python機器學習算法: 原理、實現與案例
- Java面向對象程序設計
- Mastering Linux Security and Hardening
- Visualforce Developer’s guide
- 30天學通C#項目案例開發
- 計算語言學導論