Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from...

106
These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques Gasselin de Richebourg Tim Oriol

Transcript of Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from...

Page 1: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

These are confidential sessions—please refrain from streaming, blogging, or taking pictures

Session 502

Introduction to Sprite Kit

Jacques Gasselin de RichebourgTim Oriol

Page 2: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Background

• Incredible variety of games on the store■ Iconic games—Many are 2D

•Developers have common needs■ Lots of beautiful graphics—Fast■ Particles and visual effects■ Physics and animation

• Focus on developing games instead of engines

Page 3: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Enhancing 2D game developmentSprite Kit

Images of Sprites, Shapes and Particles

Animations and Physics Audio, Video, Visual Effects

Page 4: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Enhancing 2D game developmentSprite Kit

Page 5: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Agenda

• Introduction to Sprite Kit■ Node types■ Effects and actions■ Physics

•Designing games using Sprite Kit■ Demo ‘take-home’ sample with complete documentation■ Managing the art pipeline—Creating, editing and using art■ Detailed look at Xcode support

Page 6: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

DemoAdventure

Page 7: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit Basics

Page 8: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The Parts of a Sprite Kit Game

Scenes Actions Physics

Page 9: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Scene Graph

Foreground

SKScene

Hero

Tree

TreeHUD

Life Counter

HP Bar

Background Image

Scrolling Background

Page 10: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Scene Graph

Foreground

SKScene

Hero

Tree

TreeHUD

Life Counter

HP Bar

Background Image

Scrolling Background

Nodes

Page 11: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Scene Graph

Foreground

SKScene

Hero

Tree

TreeHUD

Life Counter

HP Bar

Background Image

Scrolling Background

Actions

Physics

Nodes

Page 12: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Displaying Sprite Kit Content

Application UIView / NSView

SKView

[skView presentScene: myScene];

Scrolling Background

Tree

Background Image

HUD

Foreground

Hero

Life Counter

SKScene

Page 13: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The Sprite Kit Game Loop

-didEvaluateActions

Page 14: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The Sprite Kit Game Loop

-didEvaluateActions

Each Frame

Page 15: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The Sprite Kit Game Loop

-didEvaluateActions

-update:

Each Frame

Page 16: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The Sprite Kit Game Loop

-didEvaluateActions

-update:

SKSceneevaluates actions

Each Frame

Page 17: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The Sprite Kit Game Loop

-didEvaluateActions

-update:

SKSceneevaluates actions-didEvaluateActions

Each Frame

Page 18: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The Sprite Kit Game Loop

-didEvaluateActions

-update:

SKSceneevaluates actions-didEvaluateActions

Each Frame

SKScenesimulates physics

Page 19: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The Sprite Kit Game Loop

-didEvaluateActions

-update:

SKSceneevaluates actions-didEvaluateActions

Each Frame

-didSimulatePhysics

SKScenesimulates physics

Page 20: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The Sprite Kit Game Loop

-didEvaluateActions

-update:

SKSceneevaluates actions-didEvaluateActions

Each FrameSKViewrenders the scene

-didSimulatePhysics

SKScenesimulates physics

Page 21: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

DemoSprite Kit template

Page 22: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit Nodes

Page 23: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit Nodes

SKSpriteNode

SKCropNodeSKLabelNode

SKScene

SKEmitterNode

SKShapeNode

SKNode

SKEffectNode

Page 24: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

The basic nodeSKNode

• Used for grouping or a handle to transform children

/* Position in the parent's coordinate space */@property CGPoint position;

/* Rotation about the z axis (in radians) */@property CGFloat zRotation;

/* The scaling along the X axis */@property CGFloat xScale;

/* The scaling along the Y axis */@property CGFloat yScale;

/* Alpha (multiplied by the output color) */@property CGFloat alpha;

/* Hidden nodes (and their children) wont be rendered */@property (getter = isHidden) BOOL hidden;

SKNode

Page 25: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

•Has a explicit size• Can display a color• Can display a texture

Page 26: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

•Has a explicit size• Can display a color• Can display a texture

SKSpriteNode

Page 27: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

•Has a explicit size• Can display a color• Can display a texture

SKSpriteNode

Page 28: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

•Has a explicit size• Can display a color• Can display a texture

SKSpriteNode

Page 29: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit bitmap contentSKTexture

• Represents Sprite Kit bitmap data•Automatically managed by the framework

[SKTexture textureWithImageNamed:@"ship.png"];

[SKTexture textureWithCGImage:myCGImageRef];

[SKTexture textureWithData:rgbaNSData size:CGSizeMake(100, 100)];

[SKTexture textureWithImage:myUIImage];

[SKTexture textureWithRect:CGRectMake(100, 100, 80, 80) inTexture:tex1];

Page 30: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit bitmap contentSKTexture

• Represents Sprite Kit bitmap data•Automatically managed by the framework

[SKTexture textureWithImageNamed:@"ship.png"];

[SKTexture textureWithCGImage:myCGImageRef];

[SKTexture textureWithData:rgbaNSData size:CGSizeMake(100, 100)];

[SKTexture textureWithImage:myUIImage];

[SKTexture textureWithRect:CGRectMake(100, 100, 80, 80) inTexture:tex1];

Page 31: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKSpriteNode

/* standard sprite creation from a png file */

SKSpriteNode *sprite = [SKSpriteNode new];SKTexture *tex = [SKTexture textureWithImageNamed:@"hero.png"];sprite.texture = tex;sprite.size = tex.size;

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

Page 32: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

• Combine texture and color• colorBlendFactor

■ 0.0 is no tinting■ 1.0 is fully tinted

• Texture color is tinted• Texture alpha is multiplied• If texture is nil, color is used

colorBlendFactor

Page 33: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

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

[self addChild:sprite];

sprite.alpha = 0.5;

sprite.xScale = 0.5;

sprite.zRotation = M_PI / 4.0;

sprite.color = [SKColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];

sprite.colorBlendFactor = 1.0;

Page 34: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

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

[self addChild:sprite];

sprite.alpha = 0.5;

sprite.xScale = 0.5;

sprite.zRotation = M_PI / 4.0;

sprite.color = [SKColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];

sprite.colorBlendFactor = 1.0;

Page 35: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

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

[self addChild:sprite];

sprite.alpha = 0.5;

sprite.xScale = 0.5;

sprite.zRotation = M_PI / 4.0;

sprite.color = [SKColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];

sprite.colorBlendFactor = 1.0;

Page 36: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

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

[self addChild:sprite];

sprite.alpha = 0.5;

sprite.xScale = 0.5;

sprite.zRotation = M_PI / 4.0;

sprite.color = [SKColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];

sprite.colorBlendFactor = 1.0;

Page 37: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

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

[self addChild:sprite];

sprite.alpha = 0.5;

sprite.xScale = 0.5;

sprite.zRotation = M_PI / 4.0;

sprite.color = [SKColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];

sprite.colorBlendFactor = 1.0;

Page 38: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

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

[self addChild:sprite];

sprite.alpha = 0.5;

sprite.xScale = 0.5;

sprite.zRotation = M_PI / 4.0;

sprite.color = [SKColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];

sprite.colorBlendFactor = 1.0;

Page 39: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit MVPSKSpriteNode

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

[self addChild:sprite];

sprite.alpha = 0.5;

sprite.xScale = 0.5;

sprite.zRotation = M_PI / 4.0;

sprite.color = [SKColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:1.0];

sprite.colorBlendFactor = 1.0;

Page 40: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

For things that go boom!SKEmitterNode

• Full featured 2D particle system• Standard startValue and speed•Advanced keyframe sequence controls

Page 41: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

For things that go boom!SKEmitterNode

• Texture• Scale• Rotation• Emission angle• Emission speed• Blend modes

There are so many things to tweak!

Page 42: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKEmitterNode

•Data driven particle effects• Built-in Xcode editor• Reduce iteration time• Empower artists

Page 43: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Video as a first class spriteVideo in Games

•Until now video has been:■On top your game view■ Below your game view■ Roll your own in OpenGL

• In Sprite Kit video is truly a first class sprite

Page 44: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Video as a first class spriteSKVideoNode

• Easy one-line creation[SKVideoNode videoNodeWithVideoFileNamed:@"video.mp4"];

• Built on AVPlayer[SKVideoNode videoNodeWithAVPlayer:player];

Page 45: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKVideoNode

• Place anywhere in node tree• Run SKActions

■ Scale, fade, rotate…

• Video as a level background• Enable physics on your video

Page 46: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKShapeNode

•Dynamic shapes•Any CGPath• Built for speed• Rendered in hardware• Stroke and/or fill•Add glow effects•Multiple subpaths

Page 47: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKLabelNode

• For most text use UIKit/AppKit• Single line text as a sprite• Supports all system fonts• Supports SKActions

Page 48: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKLabelNode

• For most text use UIKit/AppKit• Single line text as a sprite• Supports all system fonts• Supports SKActions

MySKLabelNode

Page 49: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKEffectNode

• Flattens children during render■ shouldEnableEffects■ Group opacity■ Group blend modes

•Optionally apply a CIFilter• Can cache via shouldRasterize

Page 50: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKEffectNode

• Flattens children during render■ shouldEnableEffects■ Group opacity■ Group blend modes

•Optionally apply a CIFilter• Can cache via shouldRasterize

Page 51: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKCropNode

•Masks content of children•Mask defined as a SKNode

@property (retain) SKNode *maskNode

• Transparent area is masked out•Mask node can have children•Mask node can run SKActions

Page 52: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKCropNode

•Masks content of children•Mask defined as a SKNode

@property (retain) SKNode *maskNode

• Transparent area is masked out•Mask node can have children•Mask node can run SKActions

Page 53: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Actions and Animation

Page 54: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Actions Overview

SKAction *a = [SKAction rotateByAngle:M_PI duration:1.0];

•We want to design an action system that was super simple to use■ Single action class—SKAction■ One line creation■ Chainable, reusable, readable■ Actions directly affect the node it is run on

•Almost like a scripting language for Sprite Kit

Page 55: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Basic SKActions

[SKAction rotateByAngle:M_PI duration:1.0];

[SKAction moveTo:aCGPoint duration:1.0];

[SKAction fadeAlphaTo:0.75 duration:1.0];

[SKAction scaleBy:2.0 duration:1.0];

[SKAction scaleXBy:1.5 y:0.5 duration:1.0];

Page 56: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Animate your contentRunning SKActions

•Actions run immediately• Copy on add• Removed on completion

/* create an SKAction, then add it to your node */SKAction *rotate = [SKAction rotateByAngle:M_PI duration:1.0];[sprite runAction:rotate];

/* Or create your action in-line */[sprite runAction:[SKAction fadeOutWithDuration:1.0]];

Page 57: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Repeating Actions

• Repeating an action:

SKAction *spin = [SKAction rotateByAngle:2 * M_PI duration:1.0];SKAction *spinThreeTimes = [SKAction repeatAction: rotate count:3];

• Repeating an action forever:

SKAction *spinForever = [SKAction repeatActionForever:rotate];

Page 58: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Reuse the basic building blocksSequences

action1 action2 action3

SKAction Sequence

1.0 sec 2.0 sec 0.5 sec

3.5 sec

[myNode runAction: [SKAction sequence:@[action1, action2, action3]] ];

Page 59: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Reuse the basic building blocksSequences

action1 action2 action3

SKAction Sequence

1.0 sec 2.0 sec 0.5 sec

3.5 sec

NSArray Literal

[myNode runAction: [SKAction sequence:@[action1, action2, action3]] ];

Page 60: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Reuse the basic building blocksGroups

action1

action2

action3

SKAction Group

1.0 sec

2.0 sec

0.5 sec

2.0 sec

[myNode runAction: [SKAction group:@[action1, action2, action3]] ];

Page 61: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sequences of groupsCompound Actions

SKAction *group = [SKAction group:@[scale, rotate]];

[myNode runAction: [SKAction sequence:@[move, group, fadeout]] ];

move fadeout

Sequence with a Group

rotate

scale

Page 62: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Keep it simpleTiming of Actions

•No explicit timing•Utilize sequences

SKAction *wait = [SKAction waitForDuration:1.0];

[myNode runAction: [SKAction sequence:@[wait, action1]] ];

waitForDuration action1

Sequence with Wait

Page 63: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Specialty Actions

Page 64: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

AnimateSpecialty SKActions

[SKAction animateWithTextures:@[tex0, tex1, tex2] timePerFrame:0.1];

Page 65: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

AnimateSpecialty SKActions

[SKAction animateWithTextures:@[tex0, tex1, tex2] timePerFrame:0.1];

Page 66: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Follow pathSpecialty SKActions

[SKAction followPath:myPath duration:2.5]

[SKAction followPath:myPath asOffset:YES orientToPath:NO duration:5.0];

Page 67: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Follow pathSpecialty SKActions

[SKAction followPath:myPath duration:2.5]

[SKAction followPath:myPath asOffset:YES orientToPath:NO duration:5.0];

Page 68: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Follow pathSpecialty SKActions

[SKAction followPath:myPath duration:2.5]

[SKAction followPath:myPath asOffset:YES orientToPath:NO duration:5.0];

Page 69: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Remove from parentSpecialty SKActions

/* zero duration */

[SKAction removeFromParent];

/* fade out a goblin and then remove it */

SKAction *fade = [SKAction fadeOutWithDuration:1.0];SKAction *scale = [SKAction scaleBy:0.5 duration:1.0];

SKAction *group = [SKAction group:@[fade, scale]];SKAction *remove = [SKAction removeFromParent];

[goblin runAction:[SKAction sequence:@[group, remove]];

Page 70: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sound effectsSpecialty SKActions

•Great for short sound effects• Sound always plays to completion•Use AVFoundation for longer sounds or playback control

/* zero duration, starts playback */

[SKAction playSoundFileNamed:@”pew_pew.caf” waitForCompletion:NO]

/* starts playback then waits for the duration of the sound */

[SKAction playSoundFileNamed:@”kaboom.caf” waitForCompletion:YES]

Page 71: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Run blockSpecialty SKActions

/* zero duration, fires once */

[SKAction runBlock:^{ doSomething(); }]

/* show game over menu after character death animation */

SKAction *fadeOut = [SKAction fadeOutWithDuration:1.0];SKAction *remove = [SKAction removeFromParent];SKAction *showMenu = [SKAction runBlock:^{ [self showGameOverMenu]; }];

[heroSprite runAction: [SKAction sequence:@[fadeOut, showMenu, remove]] ];

Page 72: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Custom actionsSpecialty SKActions

[SKAction customActionWithDuration:dur actionBlock:^(SKNode *n, CGFloat t) { CGFloat ratio = t / dur; SKEmitterNode *en = (SKEmitterNode*)n; en.emissionAngle = ratio * 2 * M_PI;}];

Page 73: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Custom actionsSpecialty SKActions

[SKAction customActionWithDuration:dur actionBlock:^(SKNode *n, CGFloat t) { CGFloat ratio = t / dur; SKEmitterNode *en = (SKEmitterNode*)n; en.emissionAngle = ratio * 2 * M_PI;}];

Page 74: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Actions

Page 75: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

moveByX:(CGFloat)deltaX y:(CGFloat)deltaY duration:(NSTimeInterval)sec;moveTo:(CGPoint)location duration:(NSTimeInterval)sec;moveToX:(CGFloat)x duration:(NSTimeInterval)sec;moveToY:(CGFloat)y duration:(NSTimeInterval)sec;rotateByAngle:(CGFloat)radians duration:(NSTimeInterval)sec;rotateToAngle:(CGFloat)radians duration:(NSTimeInterval)sec;resizeByWidth:(CGFloat)width height:(CGFloat)height duration:(NSTimeInterval)duration;resizeToWidth:(CGFloat)width height:(CGFloat)height duration:(NSTimeInterval)duration;resizeToWidth:(CGFloat)width duration:(NSTimeInterval)duration;resizeToHeight:(CGFloat)height duration:(NSTimeInterval)duration;scaleBy:(CGFloat)scale duration:(NSTimeInterval)sec;scaleXBy:(CGFloat)xScale y:(CGFloat)yScale duration:(NSTimeInterval)sec;scaleTo:(CGFloat)scale duration:(NSTimeInterval)sec;scaleXTo:(CGFloat)xScale y:(CGFloat)yScale duration:(NSTimeInterval)sec;scaleXTo:(CGFloat)scale duration:(NSTimeInterval)sec;scaleYTo:(CGFloat)scale duration:(NSTimeInterval)sec;sequence:(NSArray *)actions;group:(NSArray *)actions;repeatAction:(SKAction *)action count:(NSUInteger)count;repeatActionForever:(SKAction *)action;fadeInWithDuration:(NSTimeInterval)sec;fadeOutWithDuration:(NSTimeInterval)sec;fadeAlphaBy:(CGFloat)factor duration:(NSTimeInterval)sec;fadeAlphaTo:(CGFloat)alpha duration:(NSTimeInterval)sec;setTexture:(SKTexture *)texture;animateWithTextures:(NSArray *)textures timePerFrame:(NSTimeInterval)sec;animateWithTextures:(NSArray *)textures timePerFrame:(NSTimeInterval)sec resize:...;playSoundFileNamed:(NSString*)soundFile waitForCompletion:(BOOL)wait;colorizeWithColor:(SKColor *)color colorBlendFactor:(CGFloat)colorBlendFactor ...;colorizeWithColorBlendFactor:(CGFloat)colorBlendFactor duration:(NSTimeInterval)sec;followPath:(CGPathRef)path duration:(NSTimeInterval)sec;followPath:(CGPathRef)path asOffset:(BOOL)offset orientToPath:(BOOL)orient ...;speedBy:(CGFloat)speed duration:(NSTimeInterval)sec;speedTo:(CGFloat)speed duration:(NSTimeInterval)sec;waitForDuration:(NSTimeInterval)sec;waitForDuration:(NSTimeInterval)sec withRange:(NSTimeInterval)durationRange;removeFromParent;performSelector:(SEL)selector onTarget:(id)target;runBlock:(dispatch_block_t)block;runBlock:(dispatch_block_t)block queue:(dispatch_queue_t)queue;runAction:(SKAction *)action onChildWithName:(NSString*)name;customActionWithDuration:(NSTimeInterval)seconds actionBlock:...;

Actions

Page 76: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Built in Physics Simulation

Page 77: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Truly integrated physicsPhysics in Sprite Kit

Game Scene Physics Simulation

Synchronize

Scrolling Background

Tree

Background Image

HUD

Foreground

Hero

Life Counter

SKScene

Page 78: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Truly integrated physicsPhysics in Sprite Kit

Game Scene and Physics

physicsBody

physicsBody

physicsBodyScrolling

Background

Tree

Background Image

HUD

Foreground

Hero

Life Counter

SKScene

Page 79: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Truly integrated physicsPhysics in Sprite Kit

• Built right into Sprite Kit•We do the synchronization•Not a global on/off switch• Enabled on a node-by-node basis•No performance penalty for what you’re not using

Page 80: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Add physics to your nodesSKPhysicsBody

/* solid circle centered at node’s anchorPoint */[SKPhysicsBody bodyWithCircleOfRadius:50];

/* hollow rect relative to node’s anchorPoint */[SKPhysicsBody bodyWithEdgeLoopFromRect:rect];

/* solid rect centered at node’s anchorPoint */[SKPhysicsBody bodyWithRectangleOfSize:size];

/* zero-width edge relative to node’s anchorPoint */[SKPhysicsBody bodyWithEdgeFromPoint:p0 toPoint:p1];

/* solid polygon relative to node’s anchorPoint */[SKPhysicsBody bodyWithPolygonFromPath:path];

/* zero-width edge relative to node’s anchorPoint */[SKPhysicsBody bodyWithEdgeChainFromPath:path];

/* hollow polygon relative to node’s anchorPoint */[SKPhysicsBody bodyWithEdgeLoopFromPath:path];

Circle

EdgeLoopFromRect

Rectangle

Edge

Polygon EdgeChain

EdgeLoopFromPath

Page 81: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Add physics to your nodesSKPhysicsBody

/* add a sprite to the scene and enable physics on it */SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"ball.png"];sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width * 0.5];

[self addChild:sprite];

/* create a circular a physicsBody */[SKPhysicsBody bodyWithCircleOfRadius:50];

/* to enable physics, set a physicsBody */mySprite.physicsBody = myPhysicsBody;

Page 82: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Add physics to your nodesSKPhysicsBody

/* add a sprite to the scene and enable physics on it */SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"ball.png"];sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width * 0.5];

[self addChild:sprite];

/* create a circular a physicsBody */[SKPhysicsBody bodyWithCircleOfRadius:50];

/* to enable physics, set a physicsBody */mySprite.physicsBody = myPhysicsBody;

Page 83: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKPhysicsBody

/* set the scene’s physicsBody to be an edge loop */self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"sphere.png"];sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width * 0.5];

[self addChild:sprite];

Add physics to your nodes

•Use an edgeLoop for a hollow rect

Page 84: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKPhysicsBody

/* set the scene’s physicsBody to be an edge loop */self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"sphere.png"];sprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:sprite.size.width * 0.5];

[self addChild:sprite];

Add physics to your nodes

•Use an edgeLoop for a hollow rect

Page 85: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKPhysicsWorld

• Each SKScene has a physicsWorld• Perform hit tests, ray casts•Add joints• Change gravity

/* default gravity, things fall down */self.physicsWorld.gravity = CGPointMake(0.0, -9.8);

/* make things fall up! */self.physicsWorld.gravity = CGPointMake(0.0, +9.8);

Page 86: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKPhysicsWorld

• Each SKScene has a physicsWorld• Perform hit tests, ray casts•Add joints• Change gravity

/* default gravity, things fall down */self.physicsWorld.gravity = CGPointMake(0.0, -9.8);

/* make things fall up! */self.physicsWorld.gravity = CGPointMake(0.0, +9.8);

Page 87: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

PhysicsWorld Contact Delegate

self.physicsWorld.contactDelegate = myContactDelegate;

@protocol SKPhysicsContactDelegate<NSObject> - (void)didBeginContact:(SKPhysicsContact *)contact; - (void)didEndContact:(SKPhysicsContact *)contact;

@end

Page 88: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

SKPhysicsContact

@interface SKPhysicsContact : NSObject /* the two physics bodies that contacted */ @property (readonly) SKPhysicsBody *bodyA; @property (readonly) SKPhysicsBody *bodyB;

/* point of first contact */ @property (readonly) CGPoint contactPoint;

/* magnitude of collision impulse at that point */ @property (readonly) CGFloat collisionImpulse;

@end

Page 89: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Collisions, Raycasts, and More

myScene.physicsWorld.contactDelegate = self;

- (void)didBeginContact:(SKPhysicsContact *)contact {if (contact.bodyA.node == heroSprite || contact.bodyB.node == heroSprite) { // Hero hit something!

}}

Page 90: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Define logical groupsCollision Groups

Player1 Player2 Baddies

Collectable Power Ups

Page 91: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Define logical groupsCollision Groups

/** Defines what logical 'categories' this body belongs too. Defaults to all bits set (all categories). */@property (assign) uint32_t categoryBitMask;

/** Defines what logical 'categories' of bodies this body responds to collisions with. Defaults to all bits set (all categories). */@property (assign) uint32_t collisionBitMask;

/** Defines what logical 'categories' of bodies this body generates intersection notifications with. Defaults to all bits cleared (no categories). */@property (assign) uint32_t contactTestBitMask;

Page 92: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Define logical groupsCollision Groups

Player1 Player2

Baddies

Collectable Power Ups

Page 93: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Define logical groupsCollision Groups

const uint32_t GOOD_GUYS = 0x1 << 0;const uint32_t BAD_GUYS = 0x1 << 1;const uint32_t POWER_UPS = 0x1 << 2;

Player1 Player2

GOOD_GUYS

Baddies BAD_GUYS

Collectable Power Ups

POWER_UPS

Page 94: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

const uint32_t GOOD_GUYS = 0x1 << 0;const uint32_t BAD_GUYS = 0x1 << 1;const uint32_t POWER_UPS = 0x1 << 2;

Define logical groupsCollision Groups

Player1 Player2

GOOD_GUYS

Baddies BAD_GUYS

Collectable Power Ups

POWER_UPS

Page 95: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

const uint32_t GOOD_GUYS = 0x1 << 0;const uint32_t BAD_GUYS = 0x1 << 1;const uint32_t POWER_UPS = 0x1 << 2;

Define logical groupsCollision Groups

Player1 Player2

GOOD_GUYS

Baddies BAD_GUYS

Collide

Collectable Power Ups

POWER_UPS

Page 96: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

const uint32_t GOOD_GUYS = 0x1 << 0;const uint32_t BAD_GUYS = 0x1 << 1;const uint32_t POWER_UPS = 0x1 << 2;

Define logical groupsCollision Groups

Player1 Player2

GOOD_GUYS

Baddies BAD_GUYS

Collide

Collide

Collectable Power Ups

POWER_UPS

Page 97: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Define logical groupsCollision Groups

Player1 Player2

GOOD_GUYS

Baddies BAD_GUYS

Collectable Power Ups

POWER_UPS

const uint32_t GOOD_GUYS = 0x1 << 0;const uint32_t BAD_GUYS = 0x1 << 1;const uint32_t POWER_UPS = 0x1 << 2;

Page 98: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Define logical groupsCollision Groups

Player1 Player2

GOOD_GUYS

Baddies BAD_GUYS

Collectable Power Ups

POWER_UPS

Contact Callback

const uint32_t GOOD_GUYS = 0x1 << 0;const uint32_t BAD_GUYS = 0x1 << 1;const uint32_t POWER_UPS = 0x1 << 2;

Page 99: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Define logical groupsCollision Groups

Player1 Player2

GOOD_GUYS

Baddies BAD_GUYS

Collectable Power Ups

POWER_UPS

Contact Callback

Contact Callback

const uint32_t GOOD_GUYS = 0x1 << 0;const uint32_t BAD_GUYS = 0x1 << 1;const uint32_t POWER_UPS = 0x1 << 2;

Page 100: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Collision Groupsconst uint32_t GOOD_GUYS = 0x1 << 0;const uint32_t BAD_GUYS = 0x1 << 1;const uint32_t POWER_UPS = 0x1 << 2;

/* players collide with bad guys, but not each other */

player1.physicsBody.categoryBitMask = GOOD_GUYS;player1.physicsBody.collisionBitMask = BAD_GUYS;player1.physicsBody.contactTestBitMask = BAD_GUYS | POWER_UPS;

player2.physicsBody.categoryBitMask = GOOD_GUYS;player2.physicsBody.collisionBitMask = BAD_GUYS;player2.physicsBody.contactTestBitMask = BAD_GUYS | POWER_UPS;

/* bad guys collide with players and other bad guys */

for (SKSpriteNode *badGuy in badGuys) { badGuy.physicsBody.categoryBitMask = BAD_GUYS; badGuy.physicsBody.collisionBitMask = BAD_GUYS | GOOD_GUYS; badGuy.physicsBody.contactTestBitMask = GOOD_GUYS;}

Page 101: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Additional Sprite Kit Features

Page 102: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Sprite Kit Features

• SKScene transitions• Reversing actions• SKView debugging stats•Automatic texture atlas creation•Applying CIFilters to SKTextures•Developer documentation• Programming guide• Code Explained: Adventure

Page 103: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Contact informationApple Evangelists

Allan SchafferGraphics and Game Technologies [email protected]

Apple Developer Forumshttp://devforums.apple.com/

Developer Documentationhttp://developer.apple.com/library/

Page 104: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Related Sessions

Designing Games with Sprite Kit MissionWednesday 2:00PM

Integrating with Game Controllers Pacific HeightsTuesday 3:15PM

Page 105: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques

Labs

Sprite Kit Lab Graphics and Games LabThursday 9:00 AM

Sprite Kit Lab Graphics and Games LabWednesday 3:15 PM

Page 106: Introduction to Sprite Kit - Apple Inc....These are confidential sessions—please refrain from streaming, blogging, or taking pictures Session 502 Introduction to Sprite Kit Jacques