Developing 2D Games With Sprite Kit
Developing 2D Games With Sprite Kit
Developing 2D Games With Sprite Kit
Allan Schaffer
Game Technologies Evangelist [email protected]
These are confidential sessionsplease refrain from streaming, blogging, or taking pictures
Sprite Kit
Enhancing 2D game development
Sprite Kit
Enhancing 2D game development
Sprite Kit
Enhancing 2D game development
Agenda
Sprites and particles Textures and atlases Scene transitions Drawing order Animation
Scene Graph
Nodes
Background
SKScene
Foreground
Background Image
Hero
Tree
Tree HP Bar
[skView presentScene:myScene];
SKScene
Scrolling Background
Background Image
Foreground
Hero
Tree
-didSimulatePhysics
Node Classes
SKNode
SKLabelNode
SKCropNode
SKEmitterNode
SKEffectNode
SKShapeNode
SKSpriteNode
SKScene
SKNode
The base class Used for grouping in node tree
@property SKNode* parent; @property NSArray* children;
SKNode
SKSpriteNode
Main rendering node type Draws a 2D image
Can be a color ! Can be a texture image ! Has an explicit size
!
SKSpriteNode
SKSpriteNode
Simple creation
SKSpriteNode *ship = [SKSpriteNode spriteNodeWithImageNamed:@ship.png]; ship.position = CGPointMake(40.0,30.0); [scene addChild:ship];
!
Creates a sprite matching the image size Centers sprite on image center (0.5, 0.5) Positions sprite at (40, 30) relative to its parent Follows UIImage -imageNamed: behavior
SKEmitterNode
Particles! Full featured 2D particle system Standard startValue and speed Advanced keyframe sequence controls
SKEmitterNode
Particle attributes Texture Scale Rotation Emission angle Emission speed Blend modes Birth rate Particle count Lifetime Position Z position Speed Size Angle Acceleration Rotation speed Scale speed Color Blend factor Alpha
SKEmitterNode
Xcode particle editor Easy-to-use particle editor
!
Integrated directly into Xcode 5 Separates design from programming Use NSKeyedUnarchiver to unarchive
Demo
Creating and loading particles
Description
Parent class of all nodes Root of the scene graph Renders a textured sprite Generates and renders particles Renders a text string Plays video content Renders a shape based on a Core Graphics path Applies a Core Image filter to its children Crops its children using a mask
SKTexture
Sprite Kit bitmap data Fundamental Sprite Kit bitmap object Very flexible
[SKTexture [SKTexture [SKTexture [SKTexture [SKTexture textureWithImageNamed:@"ship.png"]; textureWithCGImage:myCGImageRef]; textureWithData:rgbaNSData size:CGSizeMake(100, 100)]; textureWithImage:myUIImage]; textureWithRect:CGRectMake(100, 100, 80, 80) inTexture:tex1];
Properties for filter mode, mipmap generation, pre-loading Reference from a texture atlas
Texture Atlases
Key to performance Many images combined into a single larger image
Saves memory ! Improves drawing efficiency
!
big_tree_base.png big_tree_middle.png big_tree_top.png blobShadow.png cave_base.png cave_destroyed.png cave_top.png minionSplort.png small_tree_base.png small_tree_middle.png small_tree_top.png warrior_throw_hammer.png
Texture Atlases
In Sprite Kit Xcode generates atlases automatically
From individual image files ! Retina and non-retina if provided
!
SKScene
A scene of content Root node of the scene graph Displayed by a SKView Inherits from SKEffectNode Runs per-frame loop
-update: -didEvaluateActions -didSimulatePhysics
Background Image Hero
SKScene
Tree
HUD
Life Counter
SKScene
Organization Typical to create different scenes for
Main menu ! Game options ! Gameplay ! Game over
!
SKTransition
Transition between scenes Performs a transition between current and new SKScene Standard transitions
Cross fade, fade thru color ! Doors closing, doors opening ! Flip vertical, flip horizontal ! Move in, push in, reveal ! CIFilter transitions
!
SKTransition
Example
// New scene slides on top of the old scene. SKTransition *moveIn = [SKTransition moveInWithDirection:SKTransitionDirectionUp duration:2.0];
!
// Present the new scene with the transition [myView presentScene:newScene transition:moveIn];
Drawing Order
Standard behavior Two simple rules
A parent draws its content before rendering its children ! Children are rendered in the order they appear in the child array
!
Drawing Order
zPosition Gives the node an explicit height
!
Drawing Order
SKScene ignoresSiblingOrder property Allows nodes in scene to be drawn in any order
Sprite Kit batches scene by state ! Results in fewer draw calls
!
Drawing Order
Key insight Compose scene as layers
Give objects common Z value per layer ! Place overlapping objects in different layers
!
Set ignoreSiblingOrder
Actions Overview
[node runAction: [SKAction rotateByAngle:M_PI duration:1.0] ];
Basic SKActions
[SKAction rotateByAngle:M_PI duration:1.0];
! !
Compound Actions
Sequences
! !
1.0 sec
action1
2.0 sec
action2
0.5 sec
action3
Compound Actions
Groups
! !
1.0 sec 2.0 sec 0.5 sec 2.0 sec SKAction Group
Compound Actions
Sequences of groups
SKAction *group = [SKAction group:@[scale, rotate]];
!
Specialty SKActions
Animate
[SKAction animateWithTextures:@[tex0, tex1, tex2] timePerFrame:0.1];
! ! !
Specialty SKActions
Follow path
!
Specialty SKActions
Remove from parent
/* zero duration */
!
[SKAction removeFromParent];
! ! ! !
Specialty SKActions
Run block
/* zero duration, fires once */
!
SKAction *fade = [SKAction fadeOutWithDuration:1.0]; SKAction *remove = [SKAction removeFromParent]; SKAction *showMenu = [SKAction runBlock:^{ [self showGameOverMenu]; }];
!
SKActions
Large catalog
moveByX: y: duration: moveTo: duration: moveToX: duration: moveToY: duration: rotateByAngle: duration: rotateToAngle: duration: scaleXTo: duration: scaleYTo: duration: speedBy: duration: speedTo: duration: scaleBy: duration: scaleXBy: y: duration: scaleTo: duration: scaleXTo: y: duration:
sequence: group: setTexture: runBlock: runBlock: queue: removeFromParent performSelector: onTarget: resizeByWidth: height: duration: resizeToWidth: height: duration: resizeToWidth: duration: resizeToHeight: duration: repeatAction: count: repeatActionForever: fadeInWithDuration:
fadeOutWithDuration: fadeAlphaBy: duration: fadeAlphaTo: duration: animateWithTextures: timePerFrame: animateWithTextures: timePerFrame: resize: playSoundFileNamed: waitForCompletion: colorizeWithColor: colorBlendFactor: colorizeWithColorBlendFactor: duration: followPath: duration: followPath: asOffset: orientToPath: waitForDuration: waitForDuration: withRange: runAction: onChildWithName: customActionWithDuration: actionBlock:
Wrap-Up
Introduction to Sprite Kit Sprites and particles Textures and atlases Scene transitions Drawing order Animation
More Information
Allan Schaffer
Game Technologies Evangelist [email protected]
!
Developer Documentation
http://developer.apple.com/library/