Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // ColorPaperLine
- //
- // Created by MizushimaYusuke on 2017/04/23.
- // Copyright © 2017 MizushimaYusuke. All rights reserved.
- //
- import UIKit
- import SpriteKit
- class ViewController: UIViewController, SKSceneDelegate {
- weak var scene: SKScene?
- var lasttime: TimeInterval = 0
- var ready = false
- override func viewDidLoad() {
- super.viewDidLoad()
- setupScene()
- createBox()
- createCard()
- }
- func setupScene() {
- let sv = SKView(frame: view.bounds)
- let s = SKScene(size: sv.frame.size)
- s.delegate = self
- s.backgroundColor = .lightGray
- sv.presentScene(s)
- sv.showsPhysics = true
- view.addSubview(sv)
- scene = s
- }
- func createBox() {
- let box = SKShapeNode(rect: CGRect(origin: .zero, size: view.frame.size))
- box.lineWidth = 20
- box.strokeColor = UIColor(hue: 0.5, saturation: 0.2, brightness: 0.3, alpha: 1)
- scene?.addChild(box)
- box.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(x: 10, y: 10, width: view.frame.maxX - 20, height: view.frame.maxY - 20))
- box.physicsBody?.isDynamic = false
- box.physicsBody?.restitution = 1.0
- }
- func createCard() {
- let card = SKSpriteNode(color: .white, size: CGSize(width: 60, height: 60))
- card.position = view.center
- card.run(SKAction.colorize(with: .red, colorBlendFactor: 1, duration: 1))
- scene?.addChild(card)
- card.physicsBody = SKPhysicsBody(rectangleOf: card.size)
- card.physicsBody?.affectedByGravity = false
- card.physicsBody?.restitution = 1
- card.physicsBody?.angularDamping = 0
- card.name = "card"
- card.zPosition = 100
- }
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- ready = true
- if let card = scene?.childNode(withName: "card") {
- card.physicsBody?.applyImpulse(CGVector(dx: 40, dy: 40))
- card.physicsBody?.applyTorque(.pi)
- }
- }
- func update(_ currentTime: TimeInterval, for scene: SKScene) {
- if !ready {
- return
- }
- if currentTime - lasttime > 0.1 {
- lasttime = currentTime
- if let card = scene.childNode(withName: "card") {
- let newCard = SKSpriteNode(color: .white, size: CGSize(width: 60, height: 60))
- newCard.position = card.position
- newCard.zRotation = card.zRotation
- let hue = CGFloat(arc4random_uniform(10)) * 0.1
- let color = UIColor(hue: hue, saturation: 0.5, brightness:1, alpha:1)
- newCard.color = color
- scene.addChild(newCard)
- card.run(SKAction.colorize(with: color, colorBlendFactor: 1, duration: 0.2))
- card.physicsBody?.applyTorque(0.1)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement