Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // SlopeBuilder
- //
- // Created by MizushimaYusuke on 2/8/16.
- // Copyright © 2016 MizushimaYusuke. All rights reserved.
- //
- import UIKit
- import SpriteKit
- class ViewController: UIViewController {
- weak var scene : SKScene?
- override func viewDidLoad() {
- super.viewDidLoad()
- setupScene()
- createWall()
- createSlope()
- createBall()
- }
- func setupScene() {
- let sv = SKView(frame: view.bounds)
- let s = SKScene(size: sv.frame.size)
- s.backgroundColor = UIColor.blackColor()
- sv.presentScene(s)
- view.addSubview(sv)
- scene = s
- }
- func createWall() {
- for p in [CGPoint(x: 50, y: 260), CGPoint(x: CGRectGetMaxX(view.bounds) - 50, y: 260)] {
- let sideWall = SKSpriteNode(color: UIColor.whiteColor(), size: CGSize(width: 20, height: 260))
- sideWall.position = p
- scene?.addChild(sideWall)
- sideWall.physicsBody = SKPhysicsBody(rectangleOfSize: sideWall.size)
- sideWall.physicsBody?.dynamic = false
- sideWall.physicsBody?.categoryBitMask = 0x100
- }
- }
- func createSlope() {
- struct Param { var point, anchor : CGPoint }
- let params:[Param] = [
- Param(point: CGPoint(x: 50, y: 360), anchor: CGPoint(x: 0.1, y: 0.5)),
- Param(point: CGPoint(x: CGRectGetMaxX(view.bounds) - 50, y: 260), anchor: CGPoint(x: 0.9, y: 0.5))]
- for param in params {
- let slope = SKSpriteNode(color: UIColor.brownColor(), size: CGSize(width: 230, height: 20))
- slope.name = "slope"
- slope.position = param.point
- slope.anchorPoint = param.anchor
- scene?.addChild(slope)
- slope.physicsBody = SKPhysicsBody(rectangleOfSize: slope.size, center: CGPoint(x: slope.size.width * (0.5 - param.anchor.x), y: 0))
- slope.physicsBody?.dynamic = false
- slope.physicsBody?.pinned = true
- slope.physicsBody?.collisionBitMask = 0x011
- }
- }
- func createBall() {
- let ball = SKShapeNode(circleOfRadius: 20)
- ball.name = "ball"
- ball.position = CGPoint(x: 80, y: 500)
- ball.fillColor = UIColor.whiteColor()
- scene?.addChild(ball)
- ball.physicsBody = SKPhysicsBody(circleOfRadius: 20)
- ball.physicsBody?.dynamic = false
- }
- override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
- if let p = touches.first?.locationInNode(scene!) {
- let node = scene?.nodeAtPoint(p)
- if node?.name == "ball" {
- node?.physicsBody?.dynamic = true
- } else if node?.name == "slope" {
- node?.physicsBody?.dynamic = true
- delay(0.1) {
- node?.physicsBody?.dynamic = false
- }
- }
- }
- }
- final func delay(delay:Double, doit:() -> Void) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), doit)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement