Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Stefan stolniceanu spritekit, 2 d or not 2d

443 views

Published on

Stefan stolniceanu spritekit, 2 d or not 2d

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Stefan stolniceanu spritekit, 2 d or not 2d

  1. 1. Introduction to SpriteKit Ștefan Stolniceanu
  2. 2. 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
  3. 3. SpriteKit Essentials • Images of Sprites, Shapes and Particles • Animations and Physics • Audio, Video, Visual Effects • And many other What SpriteKit has to offer
  4. 4. The Parts of a Sprite Kit Game Scenes Actions Physics
  5. 5. Displaying Sprite Kit Content Application UIView / NSView SKView skView.presentScene(myScene)
  6. 6. Sprite Kit Nodes The building blocks of every 2D game
  7. 7. The father, the son, and the holy spirit SKNode SKLabelNode SKEmitterNode SKShapeNode SKSpriteNode SKEffectNode SKScene SKCropNode
  8. 8. SKNode var position: CGPoint! var zRotation: CGFloat! var xScale: CGFloat! var yScale: CGFloat! var alpha: CGFloat! var hidden: Bool!
  9. 9. 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
  10. 10. SKSpriteNode Your 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
  11. 11. SKSpriteNode Your 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
  12. 12. SKSpriteNode Your 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
  13. 13. SKTexture • Represents Sprite Kit bitmap data • Automatically managed by the framework
  14. 14. SKSpriteNode Your 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”)
  15. 15. SKEmitterNode That special something something
  16. 16. SKEmitterNode • Full featured 2D particle system • Standard startValue and speed • Advanced keyframe sequence controls That special something something
  17. 17. SKEmitterNode That special something something • Texture • Scale • Rotation • Emission angle • Emission speed • Blend modes
  18. 18. SKEmitterNode That special something something • Data driven particle effects • Built-in Xcode editor • Reduce iteration time • Empower artists
  19. 19. SKVideoNode
  20. 20. 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!”
  21. 21. SKVideoNode Video 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
  22. 22. SKVideoNode Video as a first class sprite • One-line creation let video = SKVideoNode(videoFileNamed: “video”) • Built on AVPlayer
  23. 23. SKShapeNode
  24. 24. SKShapeNode • Dynamic shapes • Any CGPath • Built for speed • Rendered in hardware • Stroke and/or fill • Add glow effects • Multiple subpaths
  25. 25. SKLabelNode
  26. 26. SKLabelNode • For most text use UIKit/AppKit • Single line text as a sprite • Supports all system fonts • Supports SKActions (of course)
  27. 27. SKEffectNode
  28. 28. SKEffectNode • Flattens children during render • shouldEnableEffects • Group opacity • Group blend modes • Optionally apply a CIFilter • Can cache via shouldRasterize
  29. 29. SKCropNode
  30. 30. 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
  31. 31. Actions and Animation
  32. 32. SKAction Super simple to use, It can be compared to a scripting language for SK
  33. 33. 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)
  34. 34. Running SKActions It’s alive! • Actions run immediately • Copy on add • Removed on completion sprite.runAction(SKAction.fadeOutWithDuration:1.0))
  35. 35. Running SKActions,
  36. 36. Running SKActions,
  37. 37. Running SKActions
  38. 38. 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)
  39. 39. Sequences Reuse already defined blocks funkyGuy.runAction(SKAction.sequence([danceSalsa, robotDance, doTheFlop])) danceSalsa robotDance doTheFlop SKAction Sequence
  40. 40. Groups Reuse already defined blocks crazyGuy.runAction(SKAction.group([danceSalsa, robotDance, doTheFlop])) danceSalsa robotDance doTheFlop SKAction Group
  41. 41. 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
  42. 42. How many, though?
  43. 43. Oh, well…
  44. 44. Built in Physics Simulation
  45. 45. Physics in Sprite Kit Truly 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
  46. 46. 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
  47. 47. 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)
  48. 48. PhysicsWorld Contact Delegate self.physicsWorld.contactDelegate = self func didBeginContact(contact: SKPhysicsContact) {} func didEndContact(contact: SKPhysicsContact) {}
  49. 49. 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!
  50. 50. Collision Groups var categoryBitMask: UInt32! var collisionBitMask: UInt32! var contactTestBitMask: UInt32!
  51. 51. Sprite Kit Game Loop Frames have it rough
  52. 52. Demo

×