52
Introduction to SpriteKit Ștefan Stolniceanu

Stefan stolniceanu spritekit, 2 d or not 2d

Embed Size (px)

Citation preview

Page 1: Stefan stolniceanu   spritekit, 2 d or not 2d

Introduction to

SpriteKit

Ștefan Stolniceanu

Page 2: Stefan stolniceanu   spritekit, 2 d or not 2d

Background

• More and more game developers emerge, all with common

needs

• Need of fast graphics

• Particles and visual effects

• Physics and animation

• Most of the iconic games are 2D

• Rechanges the focus on developing games instead of

engines

Page 3: Stefan stolniceanu   spritekit, 2 d or not 2d

SpriteKit Essentials

• Images of Sprites, Shapes and Particles

• Animations and Physics

• Audio, Video, Visual Effects

• And many other

What SpriteKit has to offer

Page 4: Stefan stolniceanu   spritekit, 2 d or not 2d

The Parts of a Sprite Kit Game

Scenes

Actions Physics

Page 5: Stefan stolniceanu   spritekit, 2 d or not 2d

Displaying Sprite Kit Content

Application UIView / NSView

SKView skView.presentScene(myScene)

Page 6: Stefan stolniceanu   spritekit, 2 d or not 2d

Sprite Kit NodesThe building blocks of every 2D game

Page 7: Stefan stolniceanu   spritekit, 2 d or not 2d

The father, the son, and the holy spirit

SKNode

SKLabelNode

SKEmitterNode

SKShapeNode SKSpriteNode

SKEffectNode

SKScene

SKCropNode

Page 8: Stefan stolniceanu   spritekit, 2 d or not 2d

SKNode

var position: CGPoint!

var zRotation: CGFloat!

var xScale: CGFloat!

var yScale: CGFloat!

var alpha: CGFloat!

var hidden: Bool!

Page 9: Stefan stolniceanu   spritekit, 2 d or not 2d

SKSpriteNode

• Has an explicit size

• Can display a colour

• Can display a texture

• You can blend the texture with a colour

Your true and only friend

Page 10: Stefan stolniceanu   spritekit, 2 d or not 2d

SKSpriteNodeYour true and only friend

SKSpriteNode

• Has an explicit size

• Can display a colour

• Can display a texture

• You can blend the texture with a colour

Page 11: Stefan stolniceanu   spritekit, 2 d or not 2d

SKSpriteNodeYour true and only friend

SKSpriteNode

• Has an explicit size

• Can display a colour

• Can display a texture

• You can blend the texture with a colour

Page 12: Stefan stolniceanu   spritekit, 2 d or not 2d

SKSpriteNodeYour true and only friend

SKSpriteNode

• Has an explicit size

• Can display a colour

• Can display a texture

• You can blend the texture with a colour

Page 13: Stefan stolniceanu   spritekit, 2 d or not 2d

SKTexture

• Represents Sprite Kit bitmap data

• Automatically managed by the framework

Page 14: Stefan stolniceanu   spritekit, 2 d or not 2d

SKSpriteNodeYour true and only friend

/* how to create a sprite from a png file */

var sprite = SKSpriteNode()

let texture = SKTexture(imageNamed:”logo”)

sprite.texture = texture

sprite.size = texture.size

VS.

var sprite = SKSpriteNode(imageNamed: “logo”)

Page 15: Stefan stolniceanu   spritekit, 2 d or not 2d

SKEmitterNodeThat special something something

Page 16: Stefan stolniceanu   spritekit, 2 d or not 2d

SKEmitterNode

• Full featured 2D particle system

• Standard startValue and speed

• Advanced keyframe sequence controls

That special something something

Page 17: Stefan stolniceanu   spritekit, 2 d or not 2d

SKEmitterNodeThat special something something

• Texture

• Scale

• Rotation

• Emission angle

• Emission speed

• Blend modes

Page 18: Stefan stolniceanu   spritekit, 2 d or not 2d

SKEmitterNodeThat special something something

• Data driven particle effects

• Built-in Xcode editor

• Reduce iteration time

• Empower artists

Page 19: Stefan stolniceanu   spritekit, 2 d or not 2d

SKVideoNode

Page 20: Stefan stolniceanu   spritekit, 2 d or not 2d

SKVideoNode

–Me

“VideoNode, VideoNode,

VideoNode does whatever a VideoNode does

Can it be less simple?

No, it can’t.

It is a Node, yes it is.

Lookout!

It is the VideoNode!”

Page 21: Stefan stolniceanu   spritekit, 2 d or not 2d

SKVideoNodeVideo as a first class sprite

• Until now video has been:• Above your game view

• Below your game view

• Roll your own in OpenGL

• Anywhere else but where you wanted it to be

• Then comes Sprite Kit, making it a first class sprite

Page 22: Stefan stolniceanu   spritekit, 2 d or not 2d

SKVideoNodeVideo as a first class sprite

• One-line creationlet video = SKVideoNode(videoFileNamed: “video”)

• Built on AVPlayer

Page 23: Stefan stolniceanu   spritekit, 2 d or not 2d

SKShapeNode

Page 24: Stefan stolniceanu   spritekit, 2 d or not 2d

SKShapeNode

• Dynamic shapes

• Any CGPath

• Built for speed

• Rendered in hardware

• Stroke and/or fill

• Add glow effects

• Multiple subpaths

Page 25: Stefan stolniceanu   spritekit, 2 d or not 2d

SKLabelNode

Page 26: Stefan stolniceanu   spritekit, 2 d or not 2d

SKLabelNode

• For most text use UIKit/AppKit

• Single line text as a sprite

• Supports all system fonts

• Supports SKActions (of course)

Page 27: Stefan stolniceanu   spritekit, 2 d or not 2d

SKEffectNode

Page 28: Stefan stolniceanu   spritekit, 2 d or not 2d

SKEffectNode

• Flattens children during render• shouldEnableEffects

• Group opacity

• Group blend modes

• Optionally apply a CIFilter

• Can cache via shouldRasterize

Page 29: Stefan stolniceanu   spritekit, 2 d or not 2d

SKCropNode

Page 30: Stefan stolniceanu   spritekit, 2 d or not 2d

SKCropNode

• Mask content of children

• Mask defined as a SKNode

• Transparent area is masked out

• Mask node can have children

• Mask node can run SKActions

Page 31: Stefan stolniceanu   spritekit, 2 d or not 2d

Actions and Animation

Page 32: Stefan stolniceanu   spritekit, 2 d or not 2d

SKAction

Super simple to use,

It can be compared to a scripting language for SK

Page 33: Stefan stolniceanu   spritekit, 2 d or not 2d

Basic SKActions

var action: SKAction!

action = SKAction.rotateByAngle(M_PI, duration: 1.0)

action = SKAction.moveTo(aCGPoint, duration: 1.0)

action = SKAction.fadeAlphaTo(0.75, duration: 1.0)

action = SKAction.scaleBy(2.0, duration: 1.0)

action = SKAction.scaleXBy(1.5, y: 0.5, duration: 1.0)

Page 34: Stefan stolniceanu   spritekit, 2 d or not 2d

Running SKActionsIt’s alive!

• Actions run immediately

• Copy on add

• Removed on completion

sprite.runAction(SKAction.fadeOutWithDuration:1.0))

Page 35: Stefan stolniceanu   spritekit, 2 d or not 2d

Running SKActions,

Page 36: Stefan stolniceanu   spritekit, 2 d or not 2d

Running SKActions,

Page 37: Stefan stolniceanu   spritekit, 2 d or not 2d

Running SKActions

Page 38: Stefan stolniceanu   spritekit, 2 d or not 2d

Running SKActions

• Repeating an action

var dance = SKAction.runBlock(dance)

var danceUntilYouDrop = SKAction.repeatAction(dance, count: 20)

• Repeating an action… forever!

var dance = SKAction.runBlock(dance)

var danceLikeYourLifeDependsOnIt = SKAction.repeatActionForever(dance)

Page 39: Stefan stolniceanu   spritekit, 2 d or not 2d

SequencesReuse already defined blocks

funkyGuy.runAction(SKAction.sequence([danceSalsa, robotDance, doTheFlop]))

danceSalsa robotDance doTheFlop

SKAction Sequence

Page 40: Stefan stolniceanu   spritekit, 2 d or not 2d

GroupsReuse already defined blocks

crazyGuy.runAction(SKAction.group([danceSalsa, robotDance, doTheFlop]))

danceSalsa

robotDance

doTheFlop

SKAction Group

Page 41: Stefan stolniceanu   spritekit, 2 d or not 2d

Sequences… of groups!Reuse already defined blocks

crazyDrunkGuy.runAction(SKAction.sequence([drinkALot, dance, regret]))

danceSalsa

robotDance

doTheFlop

SKAction Sequences of Groups

let dance = SKAction.group([danceSalsa, robotDance, doTheFlop])

regretdrinkALot

Page 42: Stefan stolniceanu   spritekit, 2 d or not 2d

How many, though?

Page 43: Stefan stolniceanu   spritekit, 2 d or not 2d

Oh, well…

Page 44: Stefan stolniceanu   spritekit, 2 d or not 2d

Built in Physics

Simulation

Page 45: Stefan stolniceanu   spritekit, 2 d or not 2d

Physics in Sprite KitTruly integrated physics

• Built right into Sprite Kit

• You don’t care about the synchronisation process

• Not a global on/off switch

• Enabled node-by-node

• No performance penalty for what you’re not using

Page 46: Stefan stolniceanu   spritekit, 2 d or not 2d

SKPhysicsBody

• Any nodes can have a PhysicsBody

• To do so… whelp… just add one

• You have some of ‘em predefined already

var aPhysicsBody = SKPhysicsBody(rectangleOfSize: 50)

aSprite.physicsBody = aPhysicsBody

Page 47: Stefan stolniceanu   spritekit, 2 d or not 2d

SKPhysicsWorld

• Each SKScene has a physicsWorld

• Perform hit tests, ray casts

• Add joints

• Change gravity

//Down!

self.physicsWorld.gravity = CGVector(0.0, -9.8)

//Up!

self.physicsWorld.gravity = CGVector(0.0, 9.8)

Page 48: Stefan stolniceanu   spritekit, 2 d or not 2d

PhysicsWorld Contact Delegate

self.physicsWorld.contactDelegate = self

func didBeginContact(contact: SKPhysicsContact) {}

func didEndContact(contact: SKPhysicsContact) {}

Page 49: Stefan stolniceanu   spritekit, 2 d or not 2d

SKPhysicsContact

/* the physics bodies involved */

var bodyA: SKPhysicsBody!

var bodyB: SKPhysicsBody!

/* point of first contact */

var contactPoint: CGPoint!

/* magnitude of collision impulse at that point */

var collisionImpulse: CGFloat!

Page 50: Stefan stolniceanu   spritekit, 2 d or not 2d

Collision Groups

var categoryBitMask: UInt32!

var collisionBitMask: UInt32!

var contactTestBitMask: UInt32!

Page 51: Stefan stolniceanu   spritekit, 2 d or not 2d

Sprite Kit Game LoopFrames have it rough

Page 52: Stefan stolniceanu   spritekit, 2 d or not 2d

Demo