Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // hatStand
- //
- // Created by MizushimaYusuke on 2017/04/27.
- // Copyright © 2017 MizushimaYusuke. All rights reserved.
- //
- import UIKit
- import SpriteKit
- class ViewController: UIViewController {
- weak var scene: SKScene?
- override func viewDidLoad() {
- super.viewDidLoad()
- setupScene()
- createStand()
- createHat()
- }
- func setupScene() {
- let sv = SKView(frame: view.bounds)
- let s = SKScene(size: sv.frame.size)
- sv.presentScene(s)
- view.addSubview(sv)
- scene = s
- }
- func createStand() {
- UIGraphicsBeginImageContextWithOptions(CGSize(width:200, height:200), false, 0)
- UIColor.white.set()
- let path = UIBezierPath()
- path.move(to: CGPoint(x: 100, y: 0))
- path.addLine(to: CGPoint(x:100, y:200))
- path.move(to: CGPoint(x:100, y:50))
- path.addLine(to: CGPoint(x: 25, y: 30))
- path.move(to: CGPoint(x:100, y:100))
- path.addLine(to: CGPoint(x: 170, y: 80))
- path.lineWidth = 5
- path.stroke()
- let img = UIGraphicsGetImageFromCurrentImageContext()!
- UIGraphicsEndImageContext()
- let texture = SKTexture(image: img)
- let node = SKSpriteNode(texture: texture)
- node.position = CGPoint(x: view.center.x, y: 100)
- scene?.addChild(node)
- node.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
- node.physicsBody?.isDynamic = false
- }
- func createHat() {
- let color = UIColor.brown
- UIGraphicsBeginImageContextWithOptions(CGSize(width:100, height:30), false, 0)
- color.set()
- let path = UIBezierPath()
- path.move(to: CGPoint(x: 0, y: 28))
- path.addLine(to: CGPoint(x: 30, y: 28))
- path.addLine(to: CGPoint(x: 30, y: 2))
- path.addLine(to: CGPoint(x: 70, y: 2))
- path.addLine(to: CGPoint(x: 70, y: 28))
- path.addLine(to: CGPoint(x: 100, y: 28))
- path.lineWidth = 4
- path.stroke()
- let img = UIGraphicsGetImageFromCurrentImageContext()!
- UIGraphicsEndImageContext()
- let texture = SKTexture(image: img)
- let node = SKSpriteNode(texture: texture)
- node.name = "hat"
- node.position = CGPoint(x: view.center.x - 200, y: view.frame.maxY - 80)
- scene?.addChild(node)
- let cover = SKSpriteNode(color: color, size: CGSize(width: 40, height: 30))
- node.addChild(cover)
- node.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
- node.physicsBody?.angularDamping = 0.8
- node.physicsBody?.restitution = 0
- node.physicsBody?.friction = 1.0
- node.physicsBody?.isDynamic = false
- node.run(SKAction.repeatForever(
- SKAction.sequence([SKAction.moveBy(x: 400, y: 0, duration: 3.0), SKAction.moveBy(x: -400, y: 0, duration: 3.0)])))
- }
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- if let hat = scene?.childNode(withName: "hat") {
- hat.name = ""
- hat.removeAllActions()
- hat.physicsBody?.isDynamic = true
- hat.run(SKAction.wait(forDuration: 1)) {
- self.createHat()
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement