Slide 1

Slide 1 text

willrax

Slide 2

Slide 2 text

Sydney, Australia

Slide 3

Slide 3 text

SKFun

Slide 4

Slide 4 text

2D Sprites and Animations

Slide 5

Slide 5 text

OSX and iOS

Slide 6

Slide 6 text

OSX and iOS

Slide 7

Slide 7 text

eleven things I wish I knew eleven months ago

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

one

Slide 10

Slide 10 text

Coordinates + +

Slide 11

Slide 11 text

Coordinates + +

Slide 12

Slide 12 text

Down Under Mode

Slide 13

Slide 13 text

two

Slide 14

Slide 14 text

One UIView

Slide 15

Slide 15 text

scene = MyScene.alloc.initWithSize(size) ! view.showsFPS = true view.presentScene(scene) class ! ! ! ! end

Slide 16

Slide 16 text

three

Slide 17

Slide 17 text

Many scenes

Slide 18

Slide 18 text

ice fire credits cut scene menus

Slide 19

Slide 19 text

MyView

Slide 20

Slide 20 text

MyView

Slide 21

Slide 21 text

class ! ! ! ! ! ! ! ! end def didMoveToView(view) super ! ! ! ! ! end add_skyline add_ground add_pipes add_bird

Slide 22

Slide 22 text

Game Loop

Slide 23

Slide 23 text

Update

Slide 24

Slide 24 text

Update Actions

Slide 25

Slide 25 text

Update Actions Physics

Slide 26

Slide 26 text

Update Actions Physics Render

Slide 27

Slide 27 text

def update end ! def didEvaluateActions end ! def didSimulatePhysics end

Slide 28

Slide 28 text

SKNode

Slide 29

Slide 29 text

Video Shape Label Particle

Slide 30

Slide 30 text

Sprite Node

Slide 31

Slide 31 text

image = "bird.png" ! bird = SKSpriteNode.spriteNodeWithImageNamed(image) bird.name = “bird" ! scene.addChild(bird)

Slide 32

Slide 32 text

four

Slide 33

Slide 33 text

Sprite Animation

Slide 34

Slide 34 text

def flap ! ! ! ! ! ! ! ! ! ! end one = SKTexture.textureWithImageNamed(“1.png") two = SKTexture.textureWithImageNamed(“2.png") three = SKTexture.textureWithImageNamed(“3.png”) textures = [one, two, three] animation = SKAction.animateWithTextures(textures, timePerFrame: 0.15) bird.repeatActionForever(animation)

Slide 35

Slide 35 text

Scrolling Images

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

SKFun

Slide 42

Slide 42 text

five

Slide 43

Slide 43 text

! Actions Are Magical "

Slide 44

Slide 44 text

def add_skyline txt = SKTexture.textureWithImageNamed(“s.png“) ! mid_x = CGRectGetMidX(self.frame) mid_y = CGRectGetMidY(self.frame) ! ! ! ! ! ! ! ! ! ! ! end position = mid_x + (i * mid_x * 2) ! sky = SKSpriteNode.spriteNodeWithTexture(txt) sky.position = CGPointMake(position, mid_y) sky.runAction scroll_action(mid_x, 0.1) ! scene.addChild(sky) 2.times do |i| ! ! ! ! ! ! ! end

Slide 45

Slide 45 text

def scroll_action(x, time) ! ! ! ! ! ! ! ! ! end width = (x * 2) dur = time * width ! move = SKAction.moveByX(-width, y: 0, duration: dur) reset = SKAction.moveByX(width, y: 0, duration: 0.0) sequence = SKAction.sequence([move, reset]) ! SKAction.repeatActionForever(sequence)

Slide 46

Slide 46 text

Parallax

Slide 47

Slide 47 text

def ! mid_x = CGRectGetMidX(self.frame) mid_y = CGRectGetMidY(self.frame) ! ! ! ! ! ! ! ! ! ! ! end position ! sky sky.position sky.runAction scroll_action(mid_x, ! scene.addChild(sky 2 ! ! ! ! ! ! ! end txt = SKTexture.textureWithImageNamed(“s.png“)

Slide 48

Slide 48 text

six

Slide 49

Slide 49 text

Textures are reusable

Slide 50

Slide 50 text

Node 1 Node 2

Slide 51

Slide 51 text

seven

Slide 52

Slide 52 text

Nodes can be added to other nodes

Slide 53

Slide 53 text

class PipePair < SKNode end

Slide 54

Slide 54 text

Up Down Pipe Pair

Slide 55

Slide 55 text

One node

Slide 56

Slide 56 text

def init super self.addChild(top_pipe) self.addChild(bottom_pipe) self end ! def top pipe = SKSpriteNode.spriteNodeWithImageNamed(“t.png”) pipe.position = CGPointMake(0, y + 450) pipe end ! def bottom pipe = SKSpriteNode.spriteNodeWithImageNamed(“b.png”) pipe.position = CGPointMake(0, y) pipe end

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

SKPhysicsWorld

Slide 59

Slide 59 text

def ! ! ! ! ! end earth = CGVectorMake(0,-5.0) ! physicsWorld.speed = 1.0 physicsWorld.gravity = earth

Slide 60

Slide 60 text

eight

Slide 61

Slide 61 text

What are CGVectors?

Slide 62

Slide 62 text

-20 -20 CGVectorMake(-20,-20)

Slide 63

Slide 63 text

20 20 CGVectorMake(20, 20)

Slide 64

Slide 64 text

2000 2000 CGVectorMake(2000, 2000)

Slide 65

Slide 65 text

Physics Body

Slide 66

Slide 66 text

nine

Slide 67

Slide 67 text

Physics bring life to your nodes

Slide 68

Slide 68 text

body = SKPhysicsBody.bodyWithRectangleOfSize(size) body.dynamic = true ! bird.physicsBody = body bird size

Slide 69

Slide 69 text

body.dynamic = false body.dynamic = true dynamic static SKPhysicsBody.bodyWithEdgeLoopFromRect(frame) edge loop

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

- touchesBegan(touches, withEvent: event) ! - touchesMoved(touches, withEvent: event) ! - touchesEnded(touches, withEvent: event) ! - touchesCancelled(touches, withEvent: event)

Slide 72

Slide 72 text

def touchesBegan(touches, withEvent: event) node = childNodeWithName "bird" ! node.physicsBody.velocity = CGVectorMake(0, 0) node.physicsBody.applyImpulse CGVectorMake(0, 8) end

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

ten

Slide 75

Slide 75 text

Collision vs Contact

Slide 76

Slide 76 text

Collision is automatic

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

Contact is voluntary

Slide 80

Slide 80 text

BIRD = 0x1 < 1 GROUND = 0x1 < 2 PIPE = 0x1 < 3 ! pipe.physicsBody.categoryBitMask = WORLD ground.physicsBody.categoryBitMask = WORLD bird.physicsBody.categoryBitMask = BIRD bird.physicsBody.contactTestBitMask = WORLD WORLD = GROUND | PIPE

Slide 81

Slide 81 text

def super ! ! ! ! end ! physicsWorld.gravity ! setup_scene class ! ! ! ! ! ! ! end physicsWorld.contactDelegate = self

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

❗️

Slide 84

Slide 84 text

def didBeginContact(contact) restart_bird reset_pipes end

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

eleven

Slide 87

Slide 87 text

controllers.

Slide 88

Slide 88 text

controllers. are awesome

Slide 89

Slide 89 text

No content

Slide 90

Slide 90 text

app.frameworks += ["GameController"] That’s it.

Slide 91

Slide 91 text

def check_controller bird = childNodeWithName("bird") ! controllers = GCController.controllers controller = controllers.first.extendedGamepad ! if controller.buttonA.isPressed? bird.physicsBody.velocity = CGVectorMake(0, 0) bird.physicsBody.applyImpulse CGVectorMake(0, 8) end end

Slide 92

Slide 92 text

remember the touch screen

Slide 93

Slide 93 text

Resources bit.ly/lynda-sk bit.ly/pragprog-sk bit.ly/apple-sk

Slide 94

Slide 94 text

questions?