Slide 1

Slide 1 text

Getting real with SK in Swift Please download the start project from www.spritekit101.info now!

Slide 2

Slide 2 text

Marin Todorov Author at raywenderlich.com, iOS consultant www.touch-code-magazine.com

Slide 3

Slide 3 text

iOS Games by Tutorials The first book on
 game development with Swift ! ! www.ios-games-by-tutorials.com

Slide 4

Slide 4 text

Game workshop in Swift • SpriteKit had huge revamp in iOS8! • Completely in Swift • New Xcode ver. 6 • New power tools

Slide 5

Slide 5 text

almost • Xcode 6 • Scene editor • Effects / lighting • Swift

Slide 6

Slide 6 text

AlienAttack • iOS8 / SpriteKit • Swift

Slide 7

Slide 7 text

The Plan Introduction ~ 15min Save 1 Tasks #1/2 ~ 10min Save 2 Task #3 ~ 10 min Save 5 Goes on … [ 15 min Break ] www.spritekit101.info

Slide 8

Slide 8 text

Epic loot www.spritekit101.info

Slide 9

Slide 9 text

Introduction ~ 15 min

Slide 10

Slide 10 text

SpriteKit project • SpriteKit.framework • integrated • built-in UIKit app (nav. controller, etc) view controller SKView

Slide 11

Slide 11 text

Scenes + +

Slide 12

Slide 12 text

Scene editor • Xcode 6 built-in • add objects • edit their properties

Slide 13

Slide 13 text

Sprites position size opacity zRotation scale children

Slide 14

Slide 14 text

Demo

Slide 15

Slide 15 text

Save #1

Slide 16

Slide 16 text

Task #1 Make chicken #2 eat Continue from Save #1 ~ 5 min

Slide 17

Slide 17 text

• Duplicate the code to connect chicken2 to the scene sprite! • Duplicate the code to create the frame animation! • Run the animation on chicken2! • [⚛] Play with timerPerFrame and repeat ~5 min

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Task #2 Add two more labels ~ 5 min

Slide 20

Slide 20 text

• Duplicate the create and configure code from waveLabel to scoreLabel! ! ! ! ! ! ! • [⚛] Play with zRotation ~5 min waveLabel ~> scoreLabel ammoLabel string “Score: \(score)” “Ammo: \(ammoCount)” h. alignment .Center .Right position.x frame.size.width/2.0 frame.size.width - 40.0

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Save #2

Slide 23

Slide 23 text

Task #3 Create an enemy ~ 10 min Continue from Save #2

Slide 24

Slide 24 text

• create new method spawnEnemy() and call it from didMoveToView()! • startPt=x: CGFloat.random(min:0, max: frame.size.width), y: frame.size.height! • fallPt = x: startPt.x, y: 160.0! • enemy = SKSpriteNode(imageNamed: “mwd1”)! • position = startPt, name = “enemy”, setScale(0.2)! • load and run frame animation on enemy with name ”mwd” and 9 steps
 
 
 
 
 ! • create and run scale animation on enemy: SKAction.scaleTo(2.0, duration: 7.0)! • create and run move animation: SKAction.moveTo(fallPt, duration: 5.0) ~10 min

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

Save #3

Slide 27

Slide 27 text

Task #4 Make the enemy run ~ 10 min Continue from Save #3

Slide 28

Slide 28 text

• create an action sequence with 
 
 three move actions and run it on
 
 enemy! • replace move animation from prev. task with the new action sequence! • replace walking frame animation with running frame animation: ~8 min fallPt walkPt runPt (startPt.x, 160.0) (startPt.x, 120.0) (startPt.x, 40.0)

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Save #4

Slide 31

Slide 31 text

Task #5 Add Explosions ~ 8 min Continue from Save #4

Slide 32

Slide 32 text

• make new method: takeHit(enemy: SKSpriteNode)! • call removeFromParent() on the passed parameter! • create explostion by calling timedEmitter(“spark”, emitTime: 0.1, totalTime: 
 
 2.0)! • set the exposition’s position to the enemy’s position! ! • back in spawEnemy(), add one more action to the action sequence:! ! ! ! • [⚛] Play with explosion.particleSpeed = [25 … 250] ~8 min

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Task #6 Flash screen ~ 8 min

Slide 35

Slide 35 text

• make new method: flash()! • create new SKSpriteNode(), set its position and size from bgNode! • set its color to: SKColor.redColor().colorWithAlphaComponent(0.5)! • add it to the scene ( via addChild )! • run this sequence on the node:! ! ! ! • call flash() from takeHit() ~6 min

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Task #7 Make some noise! ~ 5 min

Slide 38

Slide 38 text

• in your scene class preload the explosion sound action:! ! ! ! • at the bottom of takeHit() play the explosion sound:

Slide 39

Slide 39 text

Save #5

Slide 40

Slide 40 text

Break #1 Keep it tight please! 15 min

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Save #6

Slide 45

Slide 45 text

Task #8 Handle touches to shoot rocket ~ 5 min Continue from Save #6

Slide 46

Slide 46 text

! • make new method shoot(pt: CGPoint)! • print the pt param in shoot’s body! ! • override touchesEnded(touches:, event:)! • get one touch:! ! ! • finally call shoot() and pass the touch location point ~10 min

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

Task #9 Launch rocket! ~ 10 min

Slide 49

Slide 49 text

• inside the shoot() method:! • 1) if ammoCount < 1 return; 2) set ammoCount-1; 3) update ammoLabel.text! • set rocket’s zRotation to (pt - rocket.position).angle
 
 set emissionAngle to (rocket.position - pt).angle! • run scale action to 0.75 scale on rocket for 1 second! • run the following actions sequence on rocket:! ! ! ! • moveTo pt over duration of 1 second runBlock 1) create explosion at the position of rocket 2) if ammoCount > 0 call showRocket() removeFromParent ~10 min

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Save #7

Slide 52

Slide 52 text

Task #10 Create physics bodies ~ 8 min Continue from Save #7

Slide 53

Slide 53 text

• in spawnEnemy(), create a body for the enemy and set its category! ! ! ! • in showRocket(), create a body for the rocket, and set its contact category! ! ! • in GameViewController.viewDidLoad() : ~8 min

Slide 54

Slide 54 text

Task #11 Detect contact ~ 10 min

Slide 55

Slide 55 text

• make the scene class adhere to the SKPhysicsContactDelegate protocol! • in didMoveToView() set the contact delegate: 
 
 physicsWorld.contactDelegate = self! • create a new method: didBeginContact(contact: SKPhysicsContact)! • remove the hit enemy:! ! ! • set score to score + 1! • update scoreLabel with the new score ~10 min

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

Save #8

Slide 58

Slide 58 text

Break #2 Keep it tight please! 1 min 0

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

Save #9

Slide 64

Slide 64 text

Task #12 Detect when game is lost ~ 5 min Continue from Save #9

Slide 65

Slide 65 text

• declare a new variable on the scene class, which is using dynamic dispatch:! ! ! • inside checkForCompletedLevel() check if health is less than 1! • if so set wavePaused to true, finished to “lost”, and return ~5 min

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Task #13 Show end game ~ 10 min

Slide 68

Slide 68 text

• in GameViewController after you present the scene on screen, 
 
 start observing “finished”! ! ! • override observeValueForKeyPath(…) and inside add:! ! ! ! ! • ~10 min

Slide 69

Slide 69 text

Task #14 Effects! ~ 3 min

Slide 70

Slide 70 text

• inside observeValueForKeyPath(…), after presenting the alert, add:! ! ! ! ! • ~3 min

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

Save #10

Slide 73

Slide 73 text

Task #15 Check for win! ~ 10 min

Slide 74

Slide 74 text

• in the game scene class, inside checkForCompleteLevel():! ! ! ! ! • in GameViewController, inside observeValueForKeyPath():! ! ! ! • ~5 min

Slide 75

Slide 75 text

Task #16 Integrate UIKit and SpriteKit ~ 10 min

Slide 76

Slide 76 text

• in GameViewController add a gesture recognizer to the view:! ! ! ! ! ! ! • in GameScene add a new empty method swipe():! ! • ~10 min

Slide 77

Slide 77 text

Save #11 The Complete project more sounds + swipe up superpower

Slide 78

Slide 78 text

Demo

Slide 79

Slide 79 text

Review • use SceneEditor (doh!) • create and run all kind of actions on sprites • effects for effect nodes • contact detection • game logic

Slide 80

Slide 80 text

Epic loot www.spritekit101.info

Slide 81

Slide 81 text

Wrap-up slides, source code, saves www.spritekit101.info

Slide 82

Slide 82 text

www.spritekit101.info 6JCPM[QW