68
502 – Introduction to Sprite Kit 503 – Designing Games with Sprite Kit 2013/07/28 ݣLawrence Tseng [email protected]

Introduction to Sprite Kit

Embed Size (px)

Citation preview

Page 1: Introduction to Sprite Kit

502–IntroductiontoSpriteKit 503–DesigningGameswithSpriteKit

2013/07/28

Lawrence Tseng

[email protected]

Page 2: Introduction to Sprite Kit

Agenda

• Prologue

• Introduction

• SKNode

• SKAction

• PhysicEngine

• SKTexture

• ParticleSystem

• Comparew/Cocos2d

• Demo

Page 3: Introduction to Sprite Kit

PROLOGUEBeforewestart…

Page 4: Introduction to Sprite Kit

• …whatissprite?

Page 5: Introduction to Sprite Kit

Sprite

• Aspriteisatwo-dimensional

imageoranimationthatis

integratedintoalarger

scene.(Wikipedia)

Page 6: Introduction to Sprite Kit

Inthistalk…

• IntroduceSpriteKit

• Node,actions,texture,etc.

• Combinesession502&partof503

• SpriteKitv.s.Cocos2d

Page 7: Introduction to Sprite Kit

Notinthistalk…

• GameKit

• Gamedevelopmentdetailsinsession503

• Manycodeexamples

Page 8: Introduction to Sprite Kit

INTRODUCTIONBasicstructure,gameloop

Page 9: Introduction to Sprite Kit

WhatisincludedinSpriteKit?

• Imagesofsprites,shapesandparticles

• Animations

• Physics

• Audio/video

• Visualeffects

Page 10: Introduction to Sprite Kit

BasicStructure

• UIView/NSView

– SKView

• Scene

– Node

– (+Physics)

– (+Actions)

Page 11: Introduction to Sprite Kit

BasicStructure

SKScene

Background

Sky Tree

Foreground

Player Enemy

HUD

HP

AllofthemareSKNodes!

Page 12: Introduction to Sprite Kit

GameLoop

Page 13: Introduction to Sprite Kit

SPRITEKITNODESIntroductiontovariouskindsofSKNodes

Page 14: Introduction to Sprite Kit

SKNodeHierarchy

SKNode

SKLabelNode SKEmitterNode SKVideoNode SKShapeNode SKSpriteNode SKEffectNode

SKScene

SKCropNode

Page 15: Introduction to Sprite Kit

SKNode• Donotdisplayreal

things

• Useastheroleof

compositein

compositepattern

• Hasbasicproperties:

– Position

–Width&height

– Alpha

– Ishidden

– Xscaling&Yscaling

Page 16: Introduction to Sprite Kit

SKSpriteNode

• Displayspritesonthescreen

• Hasexplicitlysize

• Candisplaycolors,texture

• Textureatlassupport

Page 17: Introduction to Sprite Kit

SKSpriteNode

• Convenientone-linecreation

SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed: @"hero.png"];

Page 18: Introduction to Sprite Kit

SKSpriteNode

• Usecolorblendfactortocreatenewsprite

Page 19: Introduction to Sprite Kit

SKSpriteNode

Page 20: Introduction to Sprite Kit

SKLabelNode

• SimilarasUILabel&CCLabel

• Single-linetextasaSKNode

• Systemfonts

• Animatable

Page 21: Introduction to Sprite Kit

SKEmitterNode

• Useforparticlesystem

• Advancedkeyframesequencecontrols

• Built-inparticleeditor

Page 22: Introduction to Sprite Kit

SKVideoNode

• One-linecreationfromMP4files

• One-linecreationfromAVPlayer

• Placeanywhere,e.g.background

• Physicsenabled

[SKVideoNode videoNodeWithVideoFileNamed:@"video.mp4"];

Page 23: Introduction to Sprite Kit

SKShapeNode

• DynamicshapeswithCGPath

• Renderedinhardware

• Stroke/fill

Page 24: Introduction to Sprite Kit

SKEffectNode

• Groupopacity&groupblend

• Canbecached

• WrapwithaCIFilter

Page 25: Introduction to Sprite Kit

SKCropNode

• Usetomaskanode(anySKNode)

• SKCropNodecanhavechildren,canrun

SKActions…

Page 26: Introduction to Sprite Kit

SKACTIONActions,sequences,groups,compositions,specialty,customs

Page 27: Introduction to Sprite Kit

SKActionOverview

• Singleclass:SKAction

– AsaroleofFaçade

• Chainable,reusable,readable

• Lookslikescriptinglanguage

Page 28: Introduction to Sprite Kit

SKAction:basiccreation

Page 29: Introduction to Sprite Kit

SKAction:runningactions

• Runactionimmediately

• Copyonadd,removeoncompletion

• CanberepeatedNtimesorforever

Page 30: Introduction to Sprite Kit

SKAction:sequences

Page 31: Introduction to Sprite Kit

SKAction:groups

Page 32: Introduction to Sprite Kit

SKAction:composition

Page 33: Introduction to Sprite Kit

SKAction:timing

Page 34: Introduction to Sprite Kit

SKAction:specialty

• Animatewithtextures

• Animatewithapath(CGPath)

• Fadein/out,removefromparent

• Shortsoundsplayback

Page 35: Introduction to Sprite Kit

SKAction:custom

• Runwithblocks!

Page 36: Introduction to Sprite Kit

PHYSICENGINEPhysicsbody,physicsworld,collisiongroups

Page 37: Introduction to Sprite Kit

PhysicsSimulation

Page 38: Introduction to Sprite Kit

PhysicsSimulation

Page 39: Introduction to Sprite Kit

PhysicsSimulation

• Spritekitmanagessynchronizationdetails

• Physic-enablednodes+no-physicnodes

Page 40: Introduction to Sprite Kit

SKPhysicsBody

• Multipletypesof

shape:

– Circle

– Rectangle

– EdgeLoopFromRect

– Edge

– Polygon

– EdgeChain

– EdgeLoopFromPath

• Directlysetto

SKNode’sproperty

Page 41: Introduction to Sprite Kit

SKPhysicsWorld

• CombinewithSKScene

• Hasgravityproperty

• Managecollisiondetails

Page 42: Introduction to Sprite Kit

SKPhysicsContact

Page 43: Introduction to Sprite Kit

SKPhysicsContact

Page 44: Introduction to Sprite Kit

CollisionGroups

Page 45: Introduction to Sprite Kit

CollisionGroups

Page 46: Introduction to Sprite Kit

SKTEXTURETexturecreation,textureonSKNodes,textureatlas,textureinXcode

Page 47: Introduction to Sprite Kit

AboutSKTexture

• Representsspritebitmapdata

• Managedbyframework

Page 48: Introduction to Sprite Kit

CreateaSKTexture

Page 49: Introduction to Sprite Kit

Applytextureonnodes

Page 50: Introduction to Sprite Kit

TextureAtlas

Page 51: Introduction to Sprite Kit

WhyTextureAtlas?

• Lessmemoryconsumption

• MinimizesdiskI/O

• Speeduprenderspeed

Page 52: Introduction to Sprite Kit

CreateaTextureAtlas

• TexturePacker

• TextureAtlasToolkit

• TextureAtlasMaker

• SpriteMapper

• Xcode5!!!

Page 53: Introduction to Sprite Kit

CreateaTextureAtlas(Xcode)

1. Turnontextureatlasbuildsetting

2. PutallPNGfilesintoa.atlasdirectory(e.g.

hero.atlas)

3. DragthefolderintotheXcodeproject

4. Done!

Page 54: Introduction to Sprite Kit

MemoryConsumption

Page 55: Introduction to Sprite Kit

UsingTextureAtlas

• Noneedtodealwithplist!!

Page 56: Introduction to Sprite Kit

PARTICLESYSTEMSKEmitterNode,particleeffecteditor

Page 57: Introduction to Sprite Kit

ParticlesOverview

• Useparticlesystemforspecialeffects

• Xcodeparticlesystemeditor

• Applysequences,actionstotheparticle

Page 58: Introduction to Sprite Kit

ParticleEditor

• IntegratedintoXcode

• EditallSKEmitterNodeattributesvisually

• Don’tneedtohardcodeparticleincodes

• Eachparticleeffectisstoredina.sksfile

Page 59: Introduction to Sprite Kit

KeyframeSequences

• Controllifetimecolor/scaletransitionforeach

particle

Page 60: Introduction to Sprite Kit

ParticleAction

• Executeactionsonparticles

• Invokebyemitter

Page 61: Introduction to Sprite Kit

COCOS2DCOMPARISON

Page 62: Introduction to Sprite Kit

CCNodev.s.SKNode

• Bothimplementscompositepatternto

providetreestructure

• Similarsubclasses(leaf):CCSprite,

SKSpriteNode…

Page 63: Introduction to Sprite Kit

Actions&Animations

• Cocos2dhasmanyclassestodealwith:

CCAction,CCSpawnAction,…

• SpriteKithasonlySKActionasFaçade,thus

decreasescouplingwell

Page 64: Introduction to Sprite Kit

PhysicEngine

• Cocos2dintegrateswithBox2d/Chipmunk;

SpriteKititselfhasone

• SpriteKitphysicengineismoreintuitive

• Cocos2dprovidesvisualtoolstobuildphysic

body;InSpriteKittheycanonlybehardcoded

Page 65: Introduction to Sprite Kit

TextureAtlas

• Picture&plistfileshavetobecreatedby3rdservicesforCocos2d

• SpriteKithassuper-lazytextureatlasgenerator

– Anditprovidesoptimizationfeature,whichislockedto

payinsometools

• UnlikeCocos2d,SpriteKitdoesnothavetodealwithplistdetails

Page 66: Introduction to Sprite Kit

Misc

• Cocos2dissomewhatcross-platform;Sprite

KitonlytargetsforiOS7&OSX10.9

• Cocos2disopensource,butSpriteKitisnot

• Cocos2dhasstageeditor

Page 67: Introduction to Sprite Kit

DEMOhttps://github.com/teaualune/sk-shootduck

Page 68: Introduction to Sprite Kit

THANKYOU!