Advertisement
Guest User

kamicode

a guest
Jul 31st, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. import SpriteKit
  2. import UIKit
  3. import AVFoundation
  4.  
  5. class GameScene: SKScene {
  6.  
  7. var ninja = SKSpriteNode()
  8. var floorTexture = SKTexture()
  9. var wallDownTexture = SKTexture()
  10. var wallBreakTexture = SKTexture()
  11. var wallsMoveAndRemove = SKAction()
  12. let wallBreakName = "Wall3"
  13.  
  14. let wallGap = arc4random_uniform(20) + 75
  15.  
  16. override func didMoveToView(view: SKView) {
  17. /* Setup your scene here */
  18.  
  19. //Swipe actions
  20. var downSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
  21. downSwipe.direction = .Down
  22. view.addGestureRecognizer(downSwipe)
  23.  
  24. var upSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
  25. upSwipe.direction = .Up
  26. view.addGestureRecognizer(upSwipe)
  27.  
  28. var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
  29. rightSwipe.direction = .Right
  30. view.addGestureRecognizer(rightSwipe)
  31.  
  32. var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
  33. leftSwipe.direction = .Left
  34. view.addGestureRecognizer(leftSwipe)
  35.  
  36. //Physics
  37. self.physicsWorld.gravity = CGVectorMake(0.0, -5.0);
  38.  
  39. //Character
  40. var NinjaTexture = SKTexture(imageNamed:"ninja")
  41. NinjaTexture.filteringMode = SKTextureFilteringMode.Nearest
  42.  
  43. ninja = SKSpriteNode(texture: NinjaTexture)
  44. ninja.setScale(0.5)
  45. ninja.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.2)
  46.  
  47. ninja.physicsBody = SKPhysicsBody(circleOfRadius: ninja.size.height/2.0)
  48. ninja.physicsBody?.dynamic = true
  49. ninja.physicsBody?.allowsRotation = false
  50.  
  51. self.addChild(ninja)
  52.  
  53. //Background
  54. var backgroundTexture = SKTexture(imageNamed:"bg")
  55. var bg = SKSpriteNode(texture: backgroundTexture)
  56. bg.setScale(1.0)
  57. bg.position = CGPointMake(self.size.width/2, bg.size.height/2.0)
  58. bg.zPosition = -20
  59. self.addChild(bg)
  60.  
  61. //Ground
  62. var groundTexture = SKTexture(imageNamed:"ground")
  63. var groundSprite = SKSpriteNode(texture: groundTexture)
  64. groundSprite.setScale(2.0)
  65. groundSprite.position = CGPointMake(self.size.width/2, groundSprite.size.height/2.0) // sprite height
  66. self.addChild(groundSprite)
  67.  
  68. var ground = SKNode()
  69.  
  70. ground.position = CGPointMake(0, groundTexture.size().height)
  71. ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0)) // 2.0 sets collision height
  72.  
  73. ground.physicsBody?.dynamic = false
  74. self.addChild(ground)
  75.  
  76. //Roof
  77. var roofTexture = SKTexture(imageNamed:"roof")
  78. var roofSprite = SKSpriteNode(texture: roofTexture)
  79. roofSprite.setScale(2.0)
  80. roofSprite.position = CGPointMake(self.size.width/2, roofSprite.size.height * 3.0) // sprite height
  81. self.addChild(roofSprite)
  82.  
  83. //Walls
  84. //Create the walls
  85. floorTexture = SKTexture(imageNamed:"floor")
  86. wallDownTexture = SKTexture(imageNamed:"wall2")
  87. wallBreakTexture = SKTexture(imageNamed:"wall3")
  88.  
  89. //Movement of walls
  90. let distanceToMove = CGFloat(self.frame.size.width + 3.0 * floorTexture.size().width) //3.0 changes length before removed
  91. let moveWalls = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.003 * distanceToMove)) //0.01 how far apart walls are
  92. let removeWalls = SKAction.removeFromParent()
  93. wallsMoveAndRemove = SKAction.sequence([moveWalls, removeWalls])
  94.  
  95. //Spawn Walls
  96. let spawn = SKAction.runBlock({() in self.spawnWalls()})
  97. let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
  98. let spawnThenDelay = SKAction.sequence([spawn, delay])
  99. let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)
  100. self.runAction(spawnThenDelayForever)
  101.  
  102. //Test text
  103. var label = UILabel(frame: CGRectMake(0, 0, 200, 21))
  104. label.center = CGPointMake(325, 35) // changes postion
  105. label.textAlignment = NSTextAlignment.Center
  106. label.textColor = UIColor.redColor()
  107. label.text = "Ninja"
  108. self.view?.addSubview(label)
  109. }
  110.  
  111. func spawnWalls() {
  112.  
  113. //Wall pair lines the centers up
  114.  
  115. let wallPair = SKNode()
  116. wallPair.position = CGPointMake(self.frame.size.width + floorTexture.size().width * 2, 0)
  117. wallPair.zPosition = -10 // changes layer to back
  118.  
  119. let y = arc4random_uniform(150) // randomises the height of the floor + walls
  120.  
  121. let wallDown = SKSpriteNode(texture: wallDownTexture) //Declare wallDown
  122. wallDown.setScale(2.0) //Enlarge sprite
  123. wallDown.position = CGPointMake(0.0, CGFloat(y) + wallDown.size.height + CGFloat(wallGap))
  124.  
  125. wallDown.physicsBody = SKPhysicsBody(rectangleOfSize:wallDown.size)
  126. wallDown.physicsBody?.dynamic = false
  127. wallPair.addChild(wallDown) //adds walldown to wallpair
  128.  
  129. let wallUp = SKSpriteNode(texture: floorTexture)
  130. wallUp.setScale(2.0)
  131. wallUp.position = CGPointMake(0.0, CGFloat(y))
  132.  
  133. wallUp.physicsBody = SKPhysicsBody(rectangleOfSize:wallUp.size)
  134. wallUp.physicsBody?.dynamic = false
  135. wallPair.addChild(wallUp) //adds wallup to wallpair
  136.  
  137. wallPair.runAction(wallsMoveAndRemove) //wallsmoveandremove is a function that moves the walls across the screen and removes them once off screen
  138. self.addChild(wallPair)
  139.  
  140. let wallBreak = SKSpriteNode(texture: wallBreakTexture)
  141. wallBreak.name = "wallBreakName"
  142. wallBreak.setScale(2.0)
  143. wallBreak.position = CGPointMake(0.0, CGFloat(y))
  144.  
  145. wallBreak.physicsBody = SKPhysicsBody(rectangleOfSize:wallBreak.size)
  146. wallBreak.physicsBody?.dynamic = false
  147.  
  148. wallPair.addChild(wallBreak)
  149. self.addChild(wallBreak)
  150.  
  151. }
  152.  
  153. //Delay
  154. func delay(delay:Double, closure:()->()) {
  155. dispatch_after(
  156. dispatch_time(
  157. DISPATCH_TIME_NOW,
  158. Int64(delay * Double(NSEC_PER_SEC))
  159. ),
  160. dispatch_get_main_queue(), closure)
  161. }
  162.  
  163. //Swipe down
  164. func handleSwipes(sender:UISwipeGestureRecognizer) {
  165.  
  166. let currentPosition = ninja.position
  167.  
  168. if (sender.direction == .Down) {
  169. ninja.setScale(0.3)
  170. delay(1) {
  171. self.ninja.setScale(0.5)
  172. }
  173. }
  174. //Swipe up
  175. if (sender.direction == .Up) {
  176. ninja.physicsBody?.velocity = CGVectorMake(0, 0)
  177. ninja.physicsBody?.applyImpulse(CGVectorMake (0, 25))
  178. }
  179. //Swipe right
  180. if (sender.direction == .Right) {
  181. ninja.position.x += 50
  182. }
  183. //Swipe left
  184. if (sender.direction == .Left) {
  185. ninja.position.x -= 50
  186. }
  187. }
  188.  
  189. override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
  190. /* Called when a touch begins */
  191.  
  192. for touch: AnyObject in touches {
  193. let location = (touch as! UITouch).locationInNode(self)
  194.  
  195. if let theName = self.nodeAtPoint(location).name {
  196.  
  197. if theName == "wallBreakName" {
  198.  
  199. self.removeChildrenInArray([self.nodeAtPoint(location)])
  200.  
  201. }
  202. }
  203. }
  204. }
  205.  
  206. override func update(currentTime: CFTimeInterval) {
  207. /* Called before each frame is rendered */
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement