Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // KingyoBall
  4. //
  5. // Created by MizushimaYusuke on 2017/08/21.
  6. // Copyright © 2017 MizushimaYusuke. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SpriteKit
  11.  
  12. class ViewController: UIViewController {
  13.  
  14. weak var scene: SKScene?
  15.  
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. setupScene()
  19. createBall()
  20. }
  21.  
  22. func setupScene() {
  23. let sv = SKView(frame: view.bounds)
  24. let s = SKScene(size: sv.frame.size)
  25. s.backgroundColor = UIColor(hue: 0.4, saturation: 0.05, brightness: 1, alpha: 1)
  26. sv.presentScene(s)
  27. view.addSubview(sv)
  28. scene = s
  29. }
  30.  
  31. func createBall() {
  32. let dw = view.frame.maxX / 7.0
  33.  
  34. UIGraphicsBeginImageContextWithOptions(CGSize(width:40, height: 40), false, 0);
  35. UIColor(hue: 0, saturation: 0.8, brightness: 1, alpha: 1).set()
  36. if let ctx = UIGraphicsGetCurrentContext() {
  37. ctx.move(to: CGPoint(x: 0, y: 20))
  38. ctx.addLine(to: CGPoint(x: 25, y: 0))
  39. ctx.addLine(to: CGPoint(x: 25, y: 40))
  40. ctx.closePath()
  41. ctx.fillPath()
  42.  
  43. ctx.move(to: CGPoint(x: 20, y: 10))
  44. ctx.addLine(to: CGPoint(x: 40, y: 30))
  45. ctx.addLine(to: CGPoint(x: 40, y: 10))
  46. ctx.addLine(to: CGPoint(x: 20, y: 30))
  47. ctx.closePath()
  48.  
  49. ctx.fillPath()
  50. }
  51. let img = UIGraphicsGetImageFromCurrentImageContext()!
  52. UIGraphicsEndImageContext()
  53.  
  54. stride(from: dw, to: view.frame.maxX, by: dw).forEach { x in
  55. let ball = SKShapeNode(circleOfRadius: 40)
  56. ball.lineWidth = 2
  57. ball.strokeColor = UIColor.gray
  58. ball.position = CGPoint(x: x, y: view.frame.midY)
  59. scene?.addChild(ball)
  60.  
  61. let bar = SKSpriteNode(color: .gray, size: CGSize(width: 1, height: view.frame.midY - 80))
  62. bar.position = CGPoint(x: x, y: 3 * view.frame.maxY / 4.0)
  63. scene?.addChild(bar)
  64.  
  65. let crop = SKCropNode()
  66. let mask = SKShapeNode(circleOfRadius: 40)
  67. mask.fillColor = scene!.backgroundColor
  68. mask.lineWidth = 0
  69. crop.maskNode = mask
  70. ball.addChild(crop)
  71.  
  72. let s = SKSpriteNode(color: UIColor(hue: 0.5, saturation: 0.5, brightness: 1, alpha: 1), size: CGSize(width: 80, height: 60))
  73. s.position = CGPoint(x: 0, y: -10)
  74. crop.addChild(s)
  75.  
  76. let kingyo = SKSpriteNode(texture: SKTexture(image: img))
  77. kingyo.name = "kingyo"
  78. kingyo.position = ball.position
  79. scene?.addChild(kingyo)
  80. }
  81. }
  82.  
  83. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  84. scene?.enumerateChildNodes(withName: "kingyo", using: { (n, _) in
  85. let wait = TimeInterval(arc4random_uniform(10)) * 0.1
  86. n.run(.sequence([.wait(forDuration: wait), .move(by: CGVector(dx: 0, dy: -10), duration: 1), .move(by: CGVector(dx: 0, dy: 10), duration: 1)]))
  87. })
  88. }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement