- Swift Game Development(Third Edition)
- Siddharth Shekar Stephen Haney
- 438字
- 2021-07-16 17:37:47
Wiring up the sprite onTap events
Your games will often require the ability to run code when the player taps a specific sprite. I like to implement a system that includes all the sprites in your game so that you can add tap events to each sprite without building any additional structure. We have already implemented onTap
methods in all of our classes that adopt the GameSprite
protocol; we still need to wire up the scene to call these methods when the player taps the sprites.
Note
Before we move on, we need to remove the Core Motion code, since we will not be using it in the finished game. Once you finish exploring the Core Motion example, please remove it from the game by following the previous section's bullet points in reverse.
Implementing touchesBegan in the GameScene
SpriteKit calls our scene's touchesBegan
function every time the screen is touched. We will read the location of the touch and determine the sprite node in that position. We can check whether the touched
node adopts our GameSprite
protocol. If it does, this means it must have an onTap
function, which we can then invoke. Add the touchesBegan
function before the GameScene
class. I like to place it just after the didSimulatePhysics
function:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in (touches) { // Find the location of the touch: let location = touch.location(in: self) // Locate the node at this location: let nodeTouched = atPoint(location) // Attempt to downcast the node to the GameSprite protocol if let gameSprite = nodeTouched as? GameSprite { // If this node adheres to GameSprite, call onTap: gameSprite.onTap() } } }
That is all we need to do to wire up all of the onTap
functions we have implemented on the game object classes we have made. Of course, all of these onTap
functions are empty at the moment; we will now add some functionality to illustrate this effect.
Larger than life
Open your Player.swift
file and locate the onTap
function. Temporarily, we will expand the penguin to giant size when tapped to demonstrate that we have wired our onTap
functions correctly. Add this code inside the penguin's onTap
function:
print(" tapping on penguin")
Run the project and tap on the penguin. On the debug console, you will see the text getting printed out each time you tap on the penguin:

This example shows that our onTap
functions work. You can remove the code you added to the Player class in the onTapfunction
. We will keep the onTap wire-up code in GameScene so that we can use tap events later.
- 地方檔案與文獻研究(第2輯)
- 文獻保護中英雙解詞語手冊
- 中國古代圖書館學研究
- Learning Robotics using Python
- 中國人民大學“復印報刊資料”轉載指數排名研究報告(2014)
- 地方政府數字檔案集中管理模式研究(國家社會科學基金項目成果)
- 李一氓文存(第二卷):存在集三編下冊
- 信息資源的公共獲取機制研究
- 中國古典文獻學(東北師范大學文學院學術史文庫)
- 校勘學釋例(中國文化叢書·經典隨行)
- 春華集:中華書局員工文選(2019年)
- 茶與咖啡:張信剛文化與經濟講座
- 讓文明得到更好的傳承:公共文化服務體系中公共檔案館發展戰略
- 且為繁華寄書香:高校圖書館閱讀推廣理論與實務
- 圖書館保障弱勢群體公共信息獲取權益的對策研究