官术网_书友最值得收藏!

Putting it all together

First, we learned how to use actions to move, scale, and rotate our sprites. Then, we explored animating through multiple frames, bringing our sprite to life. Let us now combine these techniques to fly our bee back and forth across the screen, flipping the texture at each turn.

Add this code at the bottom of the didMoveToView function, beneath the bee.runAction(beeAction) line:

// Set up new actions to move our bee back and forth:
let pathLeft = SKAction.moveByX(-200, y: -10, duration: 2)
let pathRight = SKAction.moveByX(200, y: 10, duration: 2)
// These two scaleXTo actions flip the texture back and forth
// We will use these to turn the bee to face left and right
let flipTextureNegative = SKAction.scaleXTo(-1, duration: 0)
let flipTexturePositive = SKAction.scaleXTo(1, duration: 0)
// Combine actions into a cohesive flight sequence for our bee
let flightOfTheBee = SKAction.sequence([pathLeft, 
    flipTextureNegative, pathRight, flipTexturePositive])
// Last, create a looping action that will repeat forever
let neverEndingFlight = 
    SKAction.repeatActionForever(flightOfTheBee)

// Tell our bee to run the flight path, and away it goes!
bee.runAction(neverEndingFlight)

Run the project. You will see the bee flying back and forth, flapping its wings. You have officially learned the fundamentals of animation in SpriteKit! We will build on this knowledge to create a rich, animated game world for our players.

主站蜘蛛池模板: 漳浦县| 蓬溪县| 衡阳市| 长沙市| 泰兴市| 永胜县| 江西省| 湛江市| 祥云县| 岢岚县| 洪湖市| 镇远县| 嵊泗县| 江津市| 基隆市| 庆安县| 宜城市| 固始县| 广河县| 志丹县| 乐都县| 肃南| 安岳县| 临沧市| 营山县| 津市市| 南平市| 洞头县| 黄骅市| 宽城| 横山县| 长垣县| 余干县| 深水埗区| 江川县| 扶绥县| 太仓市| 兴城市| 新和县| 商河县| 白沙|