File\New\Project\..., orNew Project iOS/Game template and click Next
Pokemon2D for Product Name Swift for Language,SpriteKit for Game Technology.None for Testing System
Actions.sks GameScene.swift has only the followings:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var entities = [GKEntity]()
var graphs = [String : GKGraph]()
private var lastUpdateTime : TimeInterval = 0
override func sceneDidLoad() {
self.lastUpdateTime = 0
}
func touchDown(atPoint pos : CGPoint) {
}
func touchMoved(toPoint pos : CGPoint) {
}
func touchUp(atPoint pos : CGPoint) {
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchDown(atPoint: t.location(in: self)) }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchMoved(toPoint: t.location(in: self)) }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchUp(atPoint: t.location(in: self)) }
}
}
Pokemon2D_Assets_1.zip and drag the files into Assets
Scene: Size is iPad 12.9”GameScene.sks helloLabel
Textture and select background_grass
arrow
player_back.
player_up. You can adjust the size if you want.
name of the dpad buttons to controller_up, controller_down, controller_left, and controller_right accordingly.
Body Type to Bounding Circle, select Dynamic and deselect the allows rotation and affected by gravity options.
Player.swift) with the following codes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import Foundation
import SpriteKit
enum Direction: String {
case stop
case left
case right
case up
case down
}
class Player: SKSpriteNode {
func move(_ direction: Direction) {
print("player move: \(direction.rawValue)")
}
func stop() {
print("Stop")
}
}
GameScene.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
private var lastUpdateTime : TimeInterval = 0
private var player: Player?
override func sceneDidLoad() {
self.lastUpdateTime = 0
}
override func didMove(to view: SKView) {
player = childNode(withName: "player") as? Player
player?.move(.stop)
}
func touchDown(atPoint pos : CGPoint) {
print("touch down")
let nodeAtPoint = atPoint(pos)
if let touchedNode = nodeAtPoint as? SKSpriteNode{
if touchedNode.name?.starts(with: "controller_") == true{
let direction = touchedNode.name?.replacingOccurrences(of: "controller_", with: "")
player?.move(Direction(rawValue: direction ?? "stop")!)
}
}
}
...
move and stop inside class Player with the following contents.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func move(_ direction: Direction) {
print("player move: \(direction.rawValue)")
switch direction {
case .up:
self.physicsBody?.velocity = CGVector(dx: 0, dy: 100)
case .down:
self.physicsBody?.velocity = CGVector(dx: 0, dy: -100)
case .left:
self.physicsBody?.velocity = CGVector(dx: -100, dy: 0)
case .right:
self.physicsBody?.velocity = CGVector(dx: 100, dy: 0)
case .stop:
stop()
}
}
func stop() {
print("Stop")
self.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
}
Pokemon2D_Assets_2.zip and drag the files into Assets tree, to the game (from the assets).Add a Pokémon node (node name: pokemon), and when the player hits the Pokémon, it will print “Player hit the Pokémon” in the console.
contactDelegate and SKPhysicsContactDelegate