Game Development Reference
In-Depth Information
Chapter
14
Hit Testing and Collision Detection
So far in the game you have not moved the hero, and now you need to get him up on his
feet and moving around. You are going to keep this simple and use a one-finger touch to
move forward and two fingers to move back. To move left and right, you will use the accel-
erometer.
GameView: Moving the Hero
Start by creating a new Swift file in the project and name it
GameView.swift
. Once the
file is created, you will create a subclass from
SCNView
called
GameView
. This view will
be used in order to capture the touches from the user. If you look at
Listing 14-1
, you can
see that you are overriding the
touchesBegan
method and the
touchesEnded
meth-
od.
Listing 14-1
.
GameView.swift
import SceneKit
class GameView : SCNView {
var touchCount:Int?
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func touchesBegan(touches: NSSet, withEvent
event: UIEvent) {
var taps = event.allTouches()
touchCount = taps?.count
}
override func touchesEnded(touches: NSSet, withEvent
event: UIEvent) {
touchCount = 0