Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // SlopeBuilder
  4. //
  5. // Created by MizushimaYusuke on 2/8/16.
  6. // Copyright © 2016 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. createWall()
  20. createSlope()
  21. createBall()
  22. }
  23.  
  24. func setupScene() {
  25. let sv = SKView(frame: view.bounds)
  26. let s = SKScene(size: sv.frame.size)
  27. s.backgroundColor = UIColor.blackColor()
  28. sv.presentScene(s)
  29. view.addSubview(sv)
  30. scene = s
  31. }
  32.  
  33. func createWall() {
  34. for p in [CGPoint(x: 50, y: 260), CGPoint(x: CGRectGetMaxX(view.bounds) - 50, y: 260)] {
  35. let sideWall = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 20, height: 260))
  36. sideWall.position = p
  37. scene?.addChild(sideWall)
  38.  
  39. sideWall.physicsBody = SKPhysicsBody(rectangleOfSize: sideWall.size)
  40. sideWall.physicsBody?.dynamic = false
  41. sideWall.physicsBody?.categoryBitMask = 0x100
  42. }
  43. }
  44.  
  45. func createSlope() {
  46.  
  47. struct Param { var point, anchor : CGPoint }
  48. let params:[Param] = [
  49. Param(point: CGPoint(x: 50, y: 360), anchor: CGPoint(x: 0.1, y: 0.5)),
  50. Param(point: CGPoint(x: CGRectGetMaxX(view.bounds) - 50, y: 260), anchor: CGPoint(x: 0.9, y: 0.5))]
  51.  
  52. for param in params {
  53. let slope = SKSpriteNode(color: UIColor.brownColor(), size: CGSize(width: 230, height: 20))
  54. slope.name = "slope"
  55. slope.position = param.point
  56. slope.anchorPoint = param.anchor
  57. scene?.addChild(slope)
  58. slope.physicsBody = SKPhysicsBody(rectangleOfSize: slope.size, center: CGPoint(x: slope.size.width * (0.5 - param.anchor.x), y: 0))
  59. slope.physicsBody?.dynamic = false
  60. slope.physicsBody?.pinned = true
  61. slope.physicsBody?.collisionBitMask = 0x011
  62. }
  63. }
  64.  
  65. func createBall() {
  66. let ball = SKShapeNode(circleOfRadius: 20)
  67. ball.name = "ball"
  68. ball.position = CGPoint(x: 80, y: 500)
  69. ball.fillColor = UIColor.whiteColor()
  70. scene?.addChild(ball)
  71. ball.physicsBody = SKPhysicsBody(circleOfRadius: 20)
  72. ball.physicsBody?.dynamic = false
  73. }
  74.  
  75. override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
  76. if let p = touches.first?.locationInNode(scene!) {
  77. let node = scene?.nodeAtPoint(p)
  78. if node?.name == "ball" {
  79. node?.physicsBody?.dynamic = true
  80. } else if node?.name == "slope" {
  81. node?.physicsBody?.dynamic = true
  82. delay(0.1) {
  83. node?.physicsBody?.dynamic = false
  84. }
  85. }
  86. }
  87. }
  88.  
  89. final func delay(delay:Double, doit:() -> Void) {
  90. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), doit)
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement