Introduction To Sprite Kit PDF
Introduction To Sprite Kit PDF
Introduction To Sprite Kit PDF
Session 502
Jacques Gasselin de Richebourg
Tim Oriol
These are confidential sessions—please refrain from streaming, blogging, or taking pictures
Background
Images of Sprites, Shapes Animations and Physics Audio, Video, Visual Effects
and Particles
Sprite Kit
Enhancing 2D game development
Agenda
■ Physics
SKScene
Scrolling
Background
Foreground
Background
Image
Hero
Tree
HUD
Life
Counter
Tree
HP Bar
Scene Graph
Nodes SKScene
Scrolling
Background
Foreground
Background
Image
Hero
Tree
HUD
Life
Counter
Tree
HP Bar
Scene Graph
Nodes SKScene
Scrolling
Background Physics
Foreground
Actions
Background
Image
Hero
Tree
HUD
Life
Counter
Tree
HP Bar
Displaying Sprite Kit Content
[skView presentScene: myScene];
Application UIView / NSView
SKView
SKScene
Scrolling
Background
Foreground
Background
Image
Hero
Tree
HUD
Life
Counter
The Sprite Kit Game Loop
The Sprite Kit Game Loop
Each Frame
The Sprite Kit Game Loop
Each Frame
-update:
The Sprite Kit Game Loop
Each Frame
-update:
SKScene
evaluates actions
The Sprite Kit Game Loop
Each Frame
-update:
SKScene
-didEvaluateActions evaluates actions
The Sprite Kit Game Loop
Each Frame
-update:
SKScene
simulates physics
SKScene
-didEvaluateActions evaluates actions
The Sprite Kit Game Loop
Each Frame
-update:
-didSimulatePhysics
SKScene
simulates physics
SKScene
-didEvaluateActions evaluates actions
The Sprite Kit Game Loop
SKView Each Frame
renders the scene
-update:
-didSimulatePhysics
SKScene
simulates physics
SKScene
-didEvaluateActions evaluates actions
Demo
Sprite Kit template
Sprite Kit Nodes
Sprite Kit Nodes
SKNode
SKLabelNode SKCropNode
SKEmitterNode SKEffectNode
SKShapeNode SKSpriteNode
SKScene
SKNode
The basic node
• Used for grouping or a handle to transform children
/* Position in the parent's coordinate space */
@property CGPoint position;
SKSpriteNode
SKSpriteNode
Sprite Kit MVP
• Has a explicit size
• Can display a color
• Can display a texture
SKSpriteNode
SKSpriteNode
Sprite Kit MVP
• Has a explicit size
• Can display a color
• Can display a texture
SKSpriteNode
SKTexture
Sprite Kit bitmap content
• Represents Sprite Kit bitmap data
• Automatically managed by the framework
[SKTexture textureWithImageNamed:@"ship.png"];
[SKTexture textureWithCGImage:myCGImageRef];
[SKTexture textureWithImage:myUIImage];
[SKTexture textureWithImageNamed:@"ship.png"];
[SKTexture textureWithCGImage:myCGImageRef];
[SKTexture textureWithImage:myUIImage];
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;
SKSpriteNode
Sprite Kit MVP
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;
SKSpriteNode
Sprite Kit MVP
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;
SKSpriteNode
Sprite Kit MVP
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;
SKSpriteNode
Sprite Kit MVP
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;
SKSpriteNode
Sprite Kit MVP
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;
SKSpriteNode
Sprite Kit MVP
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;
SKEmitterNode
For things that go boom!
• Full featured 2D particle system
• Standard startValue and speed
• Advanced keyframe sequence controls
SKEmitterNode
For things that go boom!
• Texture
• Scale
• Rotation
• Emission angle
• Emission speed
• Blend modes
• Built on AVPlayer
[SKVideoNode videoNodeWithAVPlayer:player];
SKVideoNode
• Dynamic shapes
• Any CGPath
• Built for speed
• Rendered in hardware
• Stroke and/or fill
• Add glow effects
• Multiple subpaths
SKLabelNode
MySKLabelNode
SKEffectNode
■ Group opacity
■ Group blend modes
■ Group opacity
■ Group blend modes
• Repeating an action:
3.5 sec
SKAction Sequence
Sequences
Reuse the basic building blocks
NSArray Literal
3.5 sec
SKAction Sequence
Groups
Reuse the basic building blocks
2.0 sec
SKAction Group
Compound Actions
Sequences of groups
scale
move fadeout
rotate
waitForDuration action1
[SKAction removeFromParent];
SKScene Synchronize
Scrolling
Background
Foreground
Background
Image
Hero
Tree
HUD
Life
Counter
Physics in Sprite Kit
Truly integrated physics
Game Scene and Physics
SKScene
Scrolling
Background
physicsBody
Foreground
Background
Image
physicsBody
Hero
Tree
HUD
Life
Counter
physicsBody
Physics in Sprite Kit
Truly integrated physics
• 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
SKPhysicsBody
Add physics to your nodes
Circle
/* solid circle centered at node’s anchorPoint */
[SKPhysicsBody bodyWithCircleOfRadius:50];
EdgeLoopFromRect
/* hollow rect relative to node’s anchorPoint */
[SKPhysicsBody bodyWithEdgeLoopFromRect:rect];
Rectangle
/* solid rect centered at node’s anchorPoint */
[SKPhysicsBody bodyWithRectangleOfSize:size];
Edge
[self addChild:sprite];
SKPhysicsBody
Add physics to your nodes
/* create a circular a physicsBody */
[SKPhysicsBody bodyWithCircleOfRadius:50];
[self addChild:sprite];
SKPhysicsBody
Add physics to your nodes
• Use an edgeLoop for a hollow rect
[self addChild:sprite];
SKPhysicsBody
Add physics to your nodes
• Use an edgeLoop for a hollow rect
[self addChild:sprite];
SKPhysicsWorld
self.physicsWorld.contactDelegate = myContactDelegate;
@protocol SKPhysicsContactDelegate<NSObject>
- (void)didBeginContact:(SKPhysicsContact *)contact;
- (void)didEndContact:(SKPhysicsContact *)contact;
@end
SKPhysicsContact
@end
Collisions, Raycasts, and More
myScene.physicsWorld.contactDelegate = self;
- (void)didBeginContact:(SKPhysicsContact *)contact {
if (contact.bodyA.node == heroSprite || contact.bodyB.node == heroSprite)
{
// Hero hit something!
}
}
Collision Groups
Define logical groups
Baddies
Player2
Player1
/**
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;
Collision Groups
Define logical groups
Baddies
Player2
Player1
Collectable Power Ups
Collision Groups
Define logical groups
const uint32_t GOOD_GUYS = 0x1 << 0;
const uint32_t BAD_GUYS = 0x1 << 1;
const uint32_t POWER_UPS = 0x1 << 2;
Baddies
BAD_GUYS
Player2
Player1
GOOD_GUYS
Collectable Power Ups
POWER_UPS
Collision Groups
Define logical groups
const uint32_t GOOD_GUYS = 0x1 << 0;
const uint32_t BAD_GUYS = 0x1 << 1;
const uint32_t POWER_UPS = 0x1 << 2;
Baddies
BAD_GUYS
Player2
Player1
GOOD_GUYS
Collectable Power Ups
POWER_UPS
Collision Groups
Define logical groups
const uint32_t GOOD_GUYS = 0x1 << 0;
const uint32_t BAD_GUYS = 0x1 << 1;
const uint32_t POWER_UPS = 0x1 << 2;
Baddies
BAD_GUYS
i d e
ll
Co
Player2
Player1
GOOD_GUYS
Collectable Power Ups
POWER_UPS
Collision Groups Col
lide
Define logical groups
const uint32_t GOOD_GUYS = 0x1 << 0;
const uint32_t BAD_GUYS = 0x1 << 1;
const uint32_t POWER_UPS = 0x1 << 2;
Baddies
BAD_GUYS
i d e
ll
Co
Player2
Player1
GOOD_GUYS
Collectable Power Ups
POWER_UPS
Collision Groups
Define logical groups
const uint32_t GOOD_GUYS = 0x1 << 0;
const uint32_t BAD_GUYS = 0x1 << 1;
const uint32_t POWER_UPS = 0x1 << 2;
Baddies
BAD_GUYS
Player2
Player1
GOOD_GUYS
Collectable Power Ups
POWER_UPS
Collision Groups
Define logical groups
const uint32_t GOOD_GUYS = 0x1 << 0;
const uint32_t BAD_GUYS = 0x1 << 1;
const uint32_t POWER_UPS = 0x1 << 2;
Baddies
BAD_GUYS
a c k
l l b
Ca
c t
nt a
Co
Player2
Player1
GOOD_GUYS
Collectable Power Ups
POWER_UPS
Collision Groups
Define logical groups
const uint32_t GOOD_GUYS = 0x1 << 0;
const uint32_t BAD_GUYS = 0x1 << 1;
const uint32_t POWER_UPS = 0x1 << 2;
Baddies
BAD_GUYS
a c k
l l b
Ca
c t
nt a
Co
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;
• SKScene transitions
• Reversing actions
• SKView debugging stats
• Automatic texture atlas creation
• Applying CIFilters to SKTextures
• Developer documentation
• Programming guide
• Code Explained: Adventure
Apple Evangelists
Contact information
Allan Schaffer
Graphics and Game Technologies Evangelist
aschaffer@apple.com
Developer Documentation
http://developer.apple.com/library/
Related Sessions
Pacific Heights
Integrating with Game Controllers Tuesday 3:15PM
Mission
Designing Games with Sprite Kit Wednesday 2:00PM
Labs