Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // GameScene.swift
- // BreakOut
- //
- // Created by Si Brindley on 06/07/2014.
- // Copyright (c) 2014 Si Brindley. All rights reserved.
- //
- import SpriteKit
- class GameScene: SKScene, SKPhysicsContactDelegate {
- struct categories {
- static let ballCategory: UInt32 = 1 // 00000000000000000000000000000001
- static let brickCategory: UInt32 = 2 // 00000000000000000000000000000010
- static let paddleCategory: UInt32 = 4 // 00000000000000000000000000000100
- static let edgeCategory: UInt32 = 8 // 00000000000000000000000000001000
- static let bottomEdgeCategory: UInt32 = 16
- }
- let paddle = SKSpriteNode(imageNamed: "paddle")
- let playPaddleBlip = SKAction.playSoundFileNamed("blip.caf", waitForCompletion: false)
- let playBrickHit = SKAction.playSoundFileNamed("brickhit.caf", waitForCompletion: false)
- override func didMoveToView(view: SKView) {
- backgroundColor = SKColor.whiteColor()
- physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) // bouncy walls
- physicsBody.friction = 0
- physicsBody.categoryBitMask = categories.edgeCategory
- physicsWorld.gravity = CGVectorMake(0, 0) // zero-G
- physicsWorld.contactDelegate = self
- addBall()
- addPlayer()
- addBricks()
- addBottomEdge()
- }
- func didBeginContact(contact: SKPhysicsContact) -> () {
- var notTheBall: SKPhysicsBody
- if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
- notTheBall = contact.bodyB
- } else {
- notTheBall = contact.bodyA
- }
- if notTheBall.categoryBitMask == categories.brickCategory {
- runAction(playBrickHit)
- notTheBall.node.removeFromParent()
- }
- if notTheBall.categoryBitMask == categories.paddleCategory {
- runAction(playPaddleBlip)
- }
- if notTheBall.categoryBitMask == categories.bottomEdgeCategory {
- let end = EndScene.sceneWithSize(size)
- view.presentScene(end, transition: SKTransition.doorsCloseHorizontalWithDuration(0.5))
- }
- }
- override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
- for touch : AnyObject in touches {
- var location = touch.locationInNode(self)
- var newPosition = CGPointMake(location.x, 100) // paddle moves to horizonal position of touch
- // stop paddle going too far
- if newPosition.x < paddle.size.width / 2 {
- newPosition.x = paddle.size.width / 2
- }
- if newPosition.x > size.width - paddle.size.width / 2 {
- newPosition.x = size.width - paddle.size.width / 2
- }
- paddle.position = newPosition
- }
- }
- func addPlayer() -> () {
- paddle.position = CGPointMake(size.width/2, 100)
- paddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
- paddle.physicsBody.dynamic = false
- paddle.physicsBody.friction = 0
- paddle.physicsBody.categoryBitMask = categories.paddleCategory
- addChild(paddle)
- }
- func addBall() -> () {
- // Create a new sprite node from an image of a football
- let ball = SKSpriteNode(imageNamed: "ball")
- // Centre it
- ball.position = CGPoint(x: size.width/2, y: size.height/2 - 150)
- // Attach a circular physics volume to it
- ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
- // Set physical properties for ball
- ball.physicsBody.linearDamping = 0 // glides without losing speed
- ball.physicsBody.restitution = 1 // full bounciness without losing energy
- ball.physicsBody.friction = 0
- ball.physicsBody.categoryBitMask = categories.ballCategory
- // What contacts do we want to be notified about?
- ball.physicsBody.contactTestBitMask = categories.brickCategory | categories.paddleCategory | categories.bottomEdgeCategory
- // Add the football to the scene
- addChild(ball)
- // Kick ball
- var vectorHorizontal = Float(arc4random() % 30)
- vectorHorizontal = 15 - vectorHorizontal
- let kick = CGVectorMake(CGFloat(vectorHorizontal), 10)
- ball.physicsBody.applyImpulse(kick)
- }
- func addBricks() -> () {
- for row in 1...6 {
- for i in 1...4 {
- var brick = SKSpriteNode(imageNamed: "brick")
- brick.physicsBody = SKPhysicsBody(rectangleOfSize: brick.frame.size)
- brick.physicsBody.dynamic = false
- brick.physicsBody.friction = 0
- brick.physicsBody.categoryBitMask = categories.brickCategory
- var xPos: Float = Float(size.width) / 5.0 * Float(i)
- var yPos: Float = Float(size.height) - 35.0 * Float(row)
- brick.position = CGPointMake(CGFloat(xPos), CGFloat(yPos))
- addChild(brick)
- }
- }
- }
- func addBottomEdge() -> () {
- let bottomEdge = SKNode()
- bottomEdge.physicsBody = SKPhysicsBody(edgeFromPoint: CGPointMake(0, 1), toPoint: CGPointMake(size.width, 1))
- bottomEdge.physicsBody.categoryBitMask = categories.bottomEdgeCategory
- addChild(bottomEdge)
- }
- override func update(currentTime: CFTimeInterval) {
- /* Called before each frame is rendered */
- }
- }
- //
- // EndScene.swift
- // BreakOut
- //
- // Created by Si Brindley on 07/07/2014.
- // Copyright (c) 2014 Si Brindley. All rights reserved.
- //
- import SpriteKit
- class EndScene: SKScene {
- override func didMoveToView(view: SKView!) {
- // "GAME OVER"
- backgroundColor = SKColor.blackColor()
- let play = SKAction.playSoundFileNamed("gameover.caf", waitForCompletion: false)
- runAction(play)
- let label = SKLabelNode(fontNamed:"Futura Medium")
- label.text = "GAME OVER"
- label.fontColor = SKColor.whiteColor()
- label.fontSize = 44
- label.position = CGPointMake(size.width/2, size.height/2)
- addChild(label)
- // Play again button
- let tryAgain = SKLabelNode(fontNamed:"Futura Medium")
- tryAgain.text = "Tap to play again"
- tryAgain.fontColor = SKColor.whiteColor()
- tryAgain.fontSize = 24
- tryAgain.position = CGPointMake(size.width/2, -50)
- let moveLabel = SKAction.moveToY(size.height/2 - 40, duration: 1.0)
- tryAgain.runAction(moveLabel)
- addChild(tryAgain)
- }
- override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
- let firstScene = GameScene.sceneWithSize(size)
- view.presentScene(firstScene, transition: SKTransition.doorsOpenHorizontalWithDuration(1.5))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement