Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // ColorPaperLine
  4. //
  5. // Created by MizushimaYusuke on 2017/04/23.
  6. // Copyright © 2017 MizushimaYusuke. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import SpriteKit
  11.  
  12. class ViewController: UIViewController, SKSceneDelegate {
  13.  
  14. weak var scene: SKScene?
  15. var lasttime: TimeInterval = 0
  16. var ready = false
  17.  
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. setupScene()
  21. createBox()
  22. createCard()
  23. }
  24.  
  25. func setupScene() {
  26. let sv = SKView(frame: view.bounds)
  27. let s = SKScene(size: sv.frame.size)
  28. s.delegate = self
  29. s.backgroundColor = .lightGray
  30. sv.presentScene(s)
  31. sv.showsPhysics = true
  32. view.addSubview(sv)
  33. scene = s
  34. }
  35.  
  36. func createBox() {
  37. let box = SKShapeNode(rect: CGRect(origin: .zero, size: view.frame.size))
  38. box.lineWidth = 20
  39. box.strokeColor = UIColor(hue: 0.5, saturation: 0.2, brightness: 0.3, alpha: 1)
  40. scene?.addChild(box)
  41.  
  42. box.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(x: 10, y: 10, width: view.frame.maxX - 20, height: view.frame.maxY - 20))
  43. box.physicsBody?.isDynamic = false
  44. box.physicsBody?.restitution = 1.0
  45. }
  46.  
  47. func createCard() {
  48. let card = SKSpriteNode(color: .white, size: CGSize(width: 60, height: 60))
  49. card.position = view.center
  50. card.run(SKAction.colorize(with: .red, colorBlendFactor: 1, duration: 1))
  51. scene?.addChild(card)
  52.  
  53. card.physicsBody = SKPhysicsBody(rectangleOf: card.size)
  54. card.physicsBody?.affectedByGravity = false
  55. card.physicsBody?.restitution = 1
  56. card.physicsBody?.angularDamping = 0
  57. card.name = "card"
  58. card.zPosition = 100
  59.  
  60. }
  61.  
  62. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  63. ready = true
  64. if let card = scene?.childNode(withName: "card") {
  65. card.physicsBody?.applyImpulse(CGVector(dx: 40, dy: 40))
  66. card.physicsBody?.applyTorque(.pi)
  67. }
  68. }
  69.  
  70. func update(_ currentTime: TimeInterval, for scene: SKScene) {
  71. if !ready {
  72. return
  73. }
  74.  
  75. if currentTime - lasttime > 0.1 {
  76. lasttime = currentTime
  77. if let card = scene.childNode(withName: "card") {
  78.  
  79. let newCard = SKSpriteNode(color: .white, size: CGSize(width: 60, height: 60))
  80. newCard.position = card.position
  81. newCard.zRotation = card.zRotation
  82. let hue = CGFloat(arc4random_uniform(10)) * 0.1
  83. let color = UIColor(hue: hue, saturation: 0.5, brightness:1, alpha:1)
  84. newCard.color = color
  85. scene.addChild(newCard)
  86.  
  87. card.run(SKAction.colorize(with: color, colorBlendFactor: 1, duration: 0.2))
  88. card.physicsBody?.applyTorque(0.1)
  89. }
  90. }
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement