Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // hatStand
  4. //
  5. // Created by MizushimaYusuke on 2017/04/27.
  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. createStand()
  20. createHat()
  21. }
  22.  
  23. func setupScene() {
  24. let sv = SKView(frame: view.bounds)
  25. let s = SKScene(size: sv.frame.size)
  26. sv.presentScene(s)
  27. view.addSubview(sv)
  28. scene = s
  29. }
  30.  
  31. func createStand() {
  32. UIGraphicsBeginImageContextWithOptions(CGSize(width:200, height:200), false, 0)
  33. UIColor.white.set()
  34. let path = UIBezierPath()
  35. path.move(to: CGPoint(x: 100, y: 0))
  36. path.addLine(to: CGPoint(x:100, y:200))
  37. path.move(to: CGPoint(x:100, y:50))
  38. path.addLine(to: CGPoint(x: 25, y: 30))
  39.  
  40. path.move(to: CGPoint(x:100, y:100))
  41. path.addLine(to: CGPoint(x: 170, y: 80))
  42. path.lineWidth = 5
  43.  
  44. path.stroke()
  45. let img = UIGraphicsGetImageFromCurrentImageContext()!
  46. UIGraphicsEndImageContext()
  47.  
  48. let texture = SKTexture(image: img)
  49. let node = SKSpriteNode(texture: texture)
  50. node.position = CGPoint(x: view.center.x, y: 100)
  51. scene?.addChild(node)
  52.  
  53. node.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
  54. node.physicsBody?.isDynamic = false
  55. }
  56.  
  57. func createHat() {
  58. let color = UIColor.brown
  59. UIGraphicsBeginImageContextWithOptions(CGSize(width:100, height:30), false, 0)
  60. color.set()
  61. let path = UIBezierPath()
  62. path.move(to: CGPoint(x: 0, y: 28))
  63. path.addLine(to: CGPoint(x: 30, y: 28))
  64. path.addLine(to: CGPoint(x: 30, y: 2))
  65. path.addLine(to: CGPoint(x: 70, y: 2))
  66. path.addLine(to: CGPoint(x: 70, y: 28))
  67. path.addLine(to: CGPoint(x: 100, y: 28))
  68. path.lineWidth = 4
  69. path.stroke()
  70. let img = UIGraphicsGetImageFromCurrentImageContext()!
  71. UIGraphicsEndImageContext()
  72.  
  73. let texture = SKTexture(image: img)
  74. let node = SKSpriteNode(texture: texture)
  75. node.name = "hat"
  76. node.position = CGPoint(x: view.center.x - 200, y: view.frame.maxY - 80)
  77. scene?.addChild(node)
  78.  
  79. let cover = SKSpriteNode(color: color, size: CGSize(width: 40, height: 30))
  80. node.addChild(cover)
  81. node.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
  82. node.physicsBody?.angularDamping = 0.8
  83. node.physicsBody?.restitution = 0
  84. node.physicsBody?.friction = 1.0
  85. node.physicsBody?.isDynamic = false
  86.  
  87. node.run(SKAction.repeatForever(
  88. SKAction.sequence([SKAction.moveBy(x: 400, y: 0, duration: 3.0), SKAction.moveBy(x: -400, y: 0, duration: 3.0)])))
  89. }
  90.  
  91. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  92. if let hat = scene?.childNode(withName: "hat") {
  93. hat.name = ""
  94. hat.removeAllActions()
  95. hat.physicsBody?.isDynamic = true
  96. hat.run(SKAction.wait(forDuration: 1)) {
  97. self.createHat()
  98. }
  99. }
  100.  
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement