- Game Development with Swift
- Stephen Haney
- 218字
- 2021-07-16 13:45:41
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.
推薦閱讀
- Hands-On Data Structures and Algorithms with Rust
- 數據分析實戰:基于EXCEL和SPSS系列工具的實踐
- 虛擬化與云計算
- 區塊鏈通俗讀本
- Learning Proxmox VE
- 金融商業算法建模:基于Python和SAS
- MATLAB Graphics and Data Visualization Cookbook
- 計算機應用基礎教程上機指導與習題集(微課版)
- 云數據中心網絡與SDN:技術架構與實現
- MySQL數據庫技術與應用
- 機器學習:實用案例解析
- Deep Learning with R for Beginners
- 大數據分析:R基礎及應用
- 從Lucene到Elasticsearch:全文檢索實戰
- 數據中臺實戰:手把手教你搭建數據中臺