Guest User

Untitled

a guest
Jan 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. let player = SKSpriteNode(imageNamed: "player-rocket.png")
  2.  
  3. override func didMove(to view: SKView) {
  4. // this method is called when your game scene is ready to run
  5. let background = SKSpriteNode(imageNamed: "space.jpg")
  6. background.zPosition = -1
  7. addChild(background)
  8.  
  9. if let particles = SKEmitterNode(fileNamed: "SpaceDust") {
  10. particles.position.x = 512
  11. particles.advanceSimulationTime(10)
  12. addChild(particles)
  13. }
  14.  
  15. player.position.x = -400
  16. player.zPosition = 1
  17. addChild(player)
  18.  
  19. let tapRecognizer = UITapGestureRecognizer(target: self,
  20. action: #selector(remoteTapped(_:)))
  21. tapRecognizer.allowedPressTypes =
  22. [NSNumber(value: UIPressType.select.rawValue)]
  23. view.addGestureRecognizer(tapRecognizer)
  24. }
  25.  
  26.  
  27. func touchDown(atPoint pos : CGPoint) {
  28.  
  29. }
  30.  
  31. func touchMoved(toPoint pos : CGPoint) {
  32.  
  33. }
  34.  
  35. func touchUp(atPoint pos : CGPoint) {
  36.  
  37. }
  38.  
  39. // Persist the initial touch position of the remote
  40. var touchPositionY: CGFloat = 0.0
  41.  
  42. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  43. for touch in touches {
  44. touchPositionY = touch.location(in:self).y
  45. }
  46. }
  47.  
  48. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  49. for touch in touches {
  50. let location = touch.location(in: self)
  51.  
  52. if touchPositionY != 0.0 {
  53.  
  54. // Calculate the movement on the remote
  55. let deltaY = touchPositionY - location.y
  56.  
  57. // Calculate the new Sprite position
  58. var y = player.position.y - deltaY
  59.  
  60. if y > self.frame.height / 2 {
  61. y = self.frame.height / 2
  62. let rotate = SKAction.rotate(byAngle: 1.57, duration: 0.1)
  63. let move = SKAction.moveTo(x: 10, duration: 0.1)
  64. let sequence = SKAction.sequence([rotate, move])
  65. player.run(sequence)
  66. } else if y < -1 * (self.frame.height / 2) {
  67. y = -1 * (self.frame.height / 2)
  68. }
  69.  
  70. // Move the sprite
  71. player.position = CGPoint(x: -400, y: y)
  72.  
  73. }
  74. // Persist latest touch position
  75. touchPositionY = location.y
  76. }
  77. }
  78.  
  79. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  80.  
  81. }
  82.  
  83. override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
  84.  
  85. }
  86.  
  87. override func update(_ currentTime: TimeInterval) {
  88. // Called before each frame is rendered
  89. }
  90.  
  91. // tvOS Events
  92. extension GameScene {
  93. @objc func remoteTapped(
  94. _ recognizer: UITapGestureRecognizer) {
  95. //print("Tapped")
  96. } }
Add Comment
Please, Sign In to add comment