Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import SpriteKit
  2.  
  3. var contact = SKSpriteNode()
  4. var birdTexture1 = SKTexture()
  5. var scoreTexture1 = SKTexture()
  6. var birds = SKNode()
  7. var moveAndRemoveBirds = SKAction()
  8. var moving = SKNode()
  9.  
  10. let scoreCategory: UInt32 = 1 << 3
  11. let contact2Category: UInt32 = 1 << 2
  12. let crowCategory: UInt32 = 1 << 2
  13.  
  14. var scoreLabelNode = SKLabelNode()
  15. var score = NSInteger()
  16. var highScoreLabelNode = SKLabelNode()
  17. var highScore = NSInteger()
  18.  
  19.  
  20. class GameScene: SKScene {
  21. override func didMoveToView(view: SKView) {
  22.  
  23.  
  24. self.addChild(moving)
  25. moving.addChild(birds)
  26.  
  27. birdTexture1 = SKTexture(imageNamed: "crow")
  28. birdTexture1.filteringMode = SKTextureFilteringMode.Nearest
  29.  
  30. var distanceToMoveBird = CGFloat(self.frame.size.width + 2 * birdTexture1.size().width);
  31. var moveBirds = SKAction.moveByX(-distanceToMoveBird, y:0, duration:NSTimeInterval(0.0040 * distanceToMoveBird));
  32. var removeBirds = SKAction.removeFromParent();
  33. moveAndRemoveBirds = SKAction.sequence([moveBirds, removeBirds]);
  34.  
  35. var spawnBirds = SKAction.runBlock({() in self.spawnBird()})
  36. var delayBirds = SKAction.waitForDuration(NSTimeInterval(4.0))
  37. var spawnThenDelayBirds = SKAction.sequence([spawnBirds, delayBirds])
  38. var spawnThenDelayForeverBirds = SKAction.repeatActionForever(spawnThenDelayBirds)
  39. self.runAction(spawnThenDelayForeverBirds)
  40.  
  41. var scoreTexture1 = SKTexture(imageNamed: "contactNode")
  42. scoreTexture1.filteringMode = SKTextureFilteringMode.Nearest
  43.  
  44. contact = SKSpriteNode(texture: scoreTexture1)
  45. contact.position = CGPoint(x: self.frame.size.width / 3.3, y: self.frame.size.height / 2.2 )
  46. contact.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, 800))
  47. contact.physicsBody?.categoryBitMask = scoreCategory
  48. contact.physicsBody?.contactTestBitMask = crowCategory
  49. contact.physicsBody?.collisionBitMask = 0
  50. contact.physicsBody!.dynamic = false
  51. contact.physicsBody!.allowsRotation = false
  52. self.addChild(contact)
  53.  
  54. score = 0
  55. scoreLabelNode.fontName = "Helvetica-Bold"
  56. scoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.6, y: self.frame.size.height / 1.2 )
  57. scoreLabelNode.fontSize = 40
  58. scoreLabelNode.alpha = 0.7
  59. scoreLabelNode.fontColor = SKColor.redColor()
  60. scoreLabelNode.zPosition = -30
  61. scoreLabelNode.text = "Score (score)"
  62. self.addChild(scoreLabelNode)
  63.  
  64. }
  65.  
  66. func spawnBird() {
  67. var bird = SKSpriteNode()
  68. bird.position = CGPointMake( self.frame.size.width + birdTexture1.size().width * 2, 0 );
  69. var height = UInt32( self.frame.size.height / 1 )
  70. var height_max = UInt32( 500 )
  71. var height_min = UInt32( 300 )
  72. var y = arc4random_uniform(height_max - height_min + 1) + height_min;
  73. var bird1 = SKSpriteNode(texture: birdTexture1)
  74. bird1.position = CGPointMake(0.0, CGFloat(y))
  75. bird1.physicsBody = SKPhysicsBody(rectangleOfSize: bird1.size)
  76. bird1.physicsBody?.dynamic = false
  77. bird1.physicsBody?.categoryBitMask = crowCategory
  78. bird1.physicsBody?.collisionBitMask = scoreCategory
  79. bird1.physicsBody?.contactTestBitMask = scoreCategory
  80. bird.addChild(bird1)
  81.  
  82. bird.runAction(moveAndRemoveBirds)
  83.  
  84. birds.addChild(bird)
  85.  
  86. }
  87.  
  88. func didBeginContact(contact: SKPhysicsContact) {
  89.  
  90. if( moving.speed > 0 ) {
  91.  
  92. if contact.bodyA.categoryBitMask == crowCategory && contact.bodyB.categoryBitMask == scoreCategory {
  93.  
  94. score++
  95. scoreLabelNode.text = "Score (score)"
  96.  
  97.  
  98. }else {
  99.  
  100. //code removed
  101. }
  102.  
  103. }
  104.  
  105. }
  106.  
  107.  
  108. override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
  109.  
  110. }
  111.  
  112. override func update(currentTime: CFTimeInterval) {
  113.  
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement