Advertisement
Guest User

Untitled

a guest
Dec 19th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 7.15 KB | None | 0 0
  1. //
  2. //  GameScene.swift
  3. //  SpriteKitSimpleGame
  4. //
  5. //  Created by James Leist on 28/11/2016.
  6. //  Copyright © 2016 James Leist. All rights reserved.
  7. //
  8.  
  9. import SpriteKit
  10. import CoreMotion
  11.  
  12. var motionManager = CMMotionManager()
  13.  
  14. enum BodyType:UInt32 {
  15.    
  16.     case player = 1
  17.     case score = 2
  18.     case dontCollide = 4
  19.     case sides = 8
  20.    
  21. }
  22.  
  23. class GameScene: SKScene, SKPhysicsContactDelegate {
  24.    
  25.     var destX:CGFloat  = 0.0
  26.     let player = SKSpriteNode(imageNamed: "Airplane")
  27.     var bg1:SKSpriteNode!
  28.     var bg2:SKSpriteNode!
  29.     var blockLeft1:SKSpriteNode!
  30.     var blockRight1:SKSpriteNode!
  31.     var blockScore1:SKSpriteNode!
  32.     var currentScore = 0
  33.     var scoreLabel:SKLabelNode!
  34.     let playSound = SKAction.playSoundFileNamed("phaserDown3.mp3", waitForCompletion: false)
  35.    
  36.    
  37.     override func didMove(to view: SKView) {
  38.        
  39.         physicsWorld.contactDelegate = self
  40.        
  41.         backgroundColor = SKColor.white
  42.        
  43.         addScrollingBG()
  44.        
  45.         addRandomBlocks1()
  46.        
  47.         // sideRestraints()
  48.        
  49.         scoreLabel = SKLabelNode(fontNamed: "Copperplate-Bold")
  50.         scoreLabel.text = String(currentScore)
  51.         scoreLabel.fontSize = 80
  52.         scoreLabel.position = CGPoint(x: frame.size.width - 60, y: frame.size.height - 60)
  53.         scoreLabel.zPosition = 20
  54.        
  55.         self.addChild(scoreLabel)
  56.        
  57.         player.zPosition = 15
  58.        
  59.         player.position = CGPoint(x: size.width * 0.1, y: size.height * 0.5)
  60.        
  61.         // player.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Airplane"), size: CGSize(width: player.size.width, height: player.size.height))
  62.        
  63.         player.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: player.size.width, height: player.size.height))
  64.        
  65.         player.physicsBody?.affectedByGravity = false
  66.         player.physicsBody!.categoryBitMask = BodyType.player.rawValue
  67.         player.physicsBody!.contactTestBitMask = BodyType.score.rawValue | BodyType.sides.rawValue
  68.         player.physicsBody!.collisionBitMask = BodyType.sides.rawValue
  69.        
  70.        
  71.        
  72.         addChild(player)
  73.        
  74.        
  75.        
  76.     }
  77.    
  78.     func addScrollingBG() {
  79.        
  80.         bg1 = SKSpriteNode(imageNamed: "bgPlayScene")
  81.         bg1.anchorPoint = CGPoint.zero
  82.         bg1.position = CGPoint(x: 0, y: 0)
  83.         // bg1.size = CGSize(width: frame.size.width, height: frame.size.height)
  84.         bg1.zPosition = 1
  85.         addChild(bg1)
  86.        
  87.         bg2 = SKSpriteNode(imageNamed: "bgPlayScene")
  88.         bg2.anchorPoint = CGPoint.zero
  89.         bg2.position = CGPoint(x: 0, y: bg1.size.height)
  90.         // bg2.size = CGSize(width: frame.size.width, height: frame.size.height)
  91.         bg2.zPosition = 1
  92.         self.addChild(bg2)
  93.        
  94.     }
  95.    
  96.     func addRandomBlocks1() {
  97.        
  98.         let frameWidthDiv3 = UInt32(frame.size.width/3)
  99.         let randomLeftX = arc4random_uniform(frameWidthDiv3) + 50
  100.         let randomLeftWith = Int(randomLeftX)
  101.        
  102.         let frameWidthDiv12 = UInt32(frame.size.width-70)
  103.         let frameWidthDiv18 = UInt32(frame.size.width/1.8)
  104.         let randomrightX = arc4random_uniform(frameWidthDiv12 - frameWidthDiv18) + frameWidthDiv18
  105.         let randomRightWith = CGFloat(randomrightX)
  106.        
  107.         blockLeft1 = SKSpriteNode(imageNamed: "block")
  108.         blockLeft1.anchorPoint = CGPoint.zero
  109.         blockLeft1.position = CGPoint(x: 0, y: frame.size.height+150)
  110.         blockLeft1.size = CGSize(width: randomLeftWith, height: 50)
  111.         blockLeft1.zPosition = 5;
  112.         addChild(blockLeft1)
  113.        
  114.         blockRight1 = SKSpriteNode(imageNamed: "block")
  115.         blockRight1.anchorPoint = CGPoint.zero
  116.         blockRight1.position = CGPoint(x: randomRightWith, y: frame.size.height+150)
  117.         blockRight1.size = CGSize(width: frame.size.width, height: 50)
  118.         blockRight1.zPosition = 5;
  119.         addChild(blockRight1)
  120.        
  121.         blockScore1 = SKSpriteNode()
  122.         blockScore1.anchorPoint = CGPoint.zero
  123.         blockScore1.position = CGPoint(x: 0, y: blockLeft1.position.y)
  124.         blockScore1.size = CGSize(width: frame.size.width, height: 50)
  125.         blockScore1.zPosition = 4;
  126.        
  127.         blockScore1.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: blockRight1.size.width, height: blockRight1.size.height))
  128.         blockScore1.physicsBody?.affectedByGravity = false
  129.         blockScore1.physicsBody!.categoryBitMask = BodyType.score.rawValue
  130.         blockScore1.physicsBody!.contactTestBitMask = BodyType.player.rawValue
  131.         blockScore1.physicsBody!.collisionBitMask = BodyType.dontCollide.rawValue
  132.        
  133.         addChild(blockScore1)
  134.        
  135.     }
  136.    
  137.     override func update(_ currentTime: CFTimeInterval) {
  138.         /* Called before each frame is rendered */
  139.        
  140.        
  141.         let currentX = self.player.position.x
  142.        
  143.         if motionManager.isAccelerometerAvailable == true {
  144.            
  145.             motionManager.startAccelerometerUpdates(to: OperationQueue.current!, withHandler: {
  146.                
  147.                 data, error in
  148.                
  149.                 self.destX = currentX + CGFloat((data?.acceleration.x)! * 40)
  150.                
  151.                 print(CGFloat((data?.acceleration.x)!))
  152.                
  153.             })
  154.            
  155.         }
  156.  
  157.        
  158.         if (destX <= 0) {
  159.             destX = 0
  160.         }
  161.        
  162.         if (destX >= frame.size.width) {
  163.             destX = frame.size.width
  164.         }
  165.        
  166.         //let action = SKAction.moveTo(x: destX, duration: 0.1)
  167.        
  168.         //self.player.run(action)
  169.        
  170.         player.position.x = destX
  171.        
  172.         // print(destX)
  173.        
  174.         bg1.position = CGPoint(x: bg1.position.x, y: bg1.position.y-4)
  175.         bg2.position = CGPoint(x: bg2.position.x, y: bg2.position.y-4)
  176.        
  177.         if bg1.position.y < -bg1.size.height {
  178.             bg1.position = CGPoint(x: bg1.position.x, y: bg2.position.y + bg2.size.height)
  179.         }
  180.         if bg2.position.y < -bg2.size.height {
  181.             bg2.position = CGPoint(x: bg2.position.x, y: bg1.position.y + bg1.size.height)
  182.         }
  183.        
  184.         blockLeft1.position = CGPoint(x: blockLeft1.position.x, y: blockLeft1.position.y-4)
  185.         blockRight1.position = CGPoint(x: blockRight1.position.x, y: blockRight1.position.y-4)
  186.         blockScore1.position = CGPoint(x: blockScore1.position.x, y: blockScore1.position.y-4)
  187.        
  188.         if blockLeft1.position.y < -blockLeft1.size.height {
  189.             addRandomBlocks1()
  190.         }
  191.        
  192.     }
  193.    
  194.    
  195.    
  196.     func didBegin(_ contact: SKPhysicsContact) {
  197.        
  198.        
  199.         let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
  200.        
  201.         switch(contactMask) {
  202.            
  203.         case BodyType.player.rawValue | BodyType.score.rawValue:
  204.            
  205.             currentScore += 1
  206.            
  207.             run(playSound)
  208.            
  209.             scoreLabel.text = String(currentScore)
  210.             // print(currentScore)
  211.            
  212.         default:
  213.             return
  214.            
  215.         }
  216.     }
  217.    
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement