Advertisement
Guest User

Untitled

a guest
May 28th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. //
  2. // GameScene.swift
  3. // spaceshooter
  4. //
  5. //
  6.  
  7. import SpriteKit
  8.  
  9. class GameScene: SKScene {
  10.  
  11. var starShip:SKSpriteNode!
  12. var timeSinceLastFire:CGFloat = 0.0
  13. var fireWeapon:Bool = false
  14. var lastUpdateTime:NSTimeInterval = 0
  15. var delta:NSTimeInterval = NSTimeInterval(0)
  16.  
  17. override func didMoveToView(view: SKView) {
  18. /* Setup your scene here */
  19.  
  20. self.backgroundColor = SKColor.blackColor()
  21.  
  22. let starFieldEmmiter = SKEmitterNode(fileNamed: "StarField.sks")
  23. starFieldEmmiter.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame))
  24. starFieldEmmiter.zPosition = -20
  25. starFieldEmmiter.advanceSimulationTime(NSTimeInterval(starFieldEmmiter.particleLifetime))
  26.  
  27. self.addChild(starFieldEmmiter)
  28.  
  29. starShip = SKSpriteNode(imageNamed:"Spaceship")
  30. starShip.xScale = 0.2
  31. starShip.yScale = 0.2
  32.  
  33. starShip.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) + 100)
  34.  
  35. self.addChild(starShip)
  36. }
  37.  
  38. func moveShipTo(location:CGPoint)
  39. {
  40. starShip.runAction(SKAction.moveTo(location, duration: 0.05))
  41. }
  42.  
  43.  
  44. func fireProjectile(delta:CGFloat) {
  45.  
  46. if timeSinceLastFire >= 0.15
  47. {
  48. let projectile = SKSpriteNode(imageNamed:"laser1")
  49. projectile.xScale = 0.5
  50. projectile.yScale = 0.5
  51. projectile.position = starShip.position
  52. projectile.position.y += starShip.frame.height / 2
  53. projectile.zPosition = starShip.zPosition + 1
  54. let speed:CGFloat = 600.0
  55.  
  56. let distance = CGRectGetMaxY(self.frame) - starShip.position.y
  57.  
  58. let travelTime:NSTimeInterval = NSTimeInterval(distance / speed)
  59.  
  60. projectile.runAction(SKAction.moveToY(CGRectGetMaxY(self.frame) + 100, duration: travelTime))
  61.  
  62. let waitAction = SKAction.waitForDuration(travelTime)
  63. let fadeAction = SKAction.fadeOutWithDuration(1)
  64. let removeAction = SKAction.removeFromParent()
  65. let sequence = [waitAction, fadeAction, removeAction]
  66.  
  67. self.addChild(projectile)
  68.  
  69. projectile.runAction(SKAction.sequence(sequence))
  70.  
  71. timeSinceLastFire = 0.0
  72. }
  73.  
  74. timeSinceLastFire += delta
  75. }
  76.  
  77. override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
  78. /* Called when a touch begins */
  79.  
  80. for touch in (touches as! Set<UITouch>) {
  81. let location = touch.locationInNode(self)
  82.  
  83. moveShipTo(location)
  84. self.fireWeapon = true
  85. }
  86. }
  87.  
  88. override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
  89.  
  90. for touch in (touches as! Set<UITouch>) {
  91. let location = touch.locationInNode(self)
  92.  
  93. moveShipTo(location)
  94. }
  95. }
  96.  
  97. override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
  98. for touch in (touches as! Set<UITouch>) {
  99. let location = touch.locationInNode(self)
  100.  
  101. self.fireWeapon = false
  102. }
  103. }
  104.  
  105. override func update(currentTime: CFTimeInterval) {
  106. /* Called before each frame is rendered */
  107.  
  108. if lastUpdateTime == 0.0 {
  109. delta = 0
  110. } else {
  111. delta = currentTime - lastUpdateTime
  112. }
  113.  
  114. lastUpdateTime = currentTime
  115.  
  116. if fireWeapon
  117. {
  118. self.fireProjectile(CGFloat(delta))
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement