Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ViewController.swift
- // KingyoBall
- //
- // Created by MizushimaYusuke on 2017/08/21.
- // Copyright © 2017 MizushimaYusuke. All rights reserved.
- //
- import UIKit
- import SpriteKit
- class ViewController: UIViewController {
- weak var scene: SKScene?
- override func viewDidLoad() {
- super.viewDidLoad()
- setupScene()
- createBall()
- }
- func setupScene() {
- let sv = SKView(frame: view.bounds)
- let s = SKScene(size: sv.frame.size)
- s.backgroundColor = UIColor(hue: 0.4, saturation: 0.05, brightness: 1, alpha: 1)
- sv.presentScene(s)
- view.addSubview(sv)
- scene = s
- }
- func createBall() {
- let dw = view.frame.maxX / 7.0
- UIGraphicsBeginImageContextWithOptions(CGSize(width:40, height: 40), false, 0);
- UIColor(hue: 0, saturation: 0.8, brightness: 1, alpha: 1).set()
- if let ctx = UIGraphicsGetCurrentContext() {
- ctx.move(to: CGPoint(x: 0, y: 20))
- ctx.addLine(to: CGPoint(x: 25, y: 0))
- ctx.addLine(to: CGPoint(x: 25, y: 40))
- ctx.closePath()
- ctx.fillPath()
- ctx.move(to: CGPoint(x: 20, y: 10))
- ctx.addLine(to: CGPoint(x: 40, y: 30))
- ctx.addLine(to: CGPoint(x: 40, y: 10))
- ctx.addLine(to: CGPoint(x: 20, y: 30))
- ctx.closePath()
- ctx.fillPath()
- }
- let img = UIGraphicsGetImageFromCurrentImageContext()!
- UIGraphicsEndImageContext()
- stride(from: dw, to: view.frame.maxX, by: dw).forEach { x in
- let ball = SKShapeNode(circleOfRadius: 40)
- ball.lineWidth = 2
- ball.strokeColor = UIColor.gray
- ball.position = CGPoint(x: x, y: view.frame.midY)
- scene?.addChild(ball)
- let bar = SKSpriteNode(color: .gray, size: CGSize(width: 1, height: view.frame.midY - 80))
- bar.position = CGPoint(x: x, y: 3 * view.frame.maxY / 4.0)
- scene?.addChild(bar)
- let crop = SKCropNode()
- let mask = SKShapeNode(circleOfRadius: 40)
- mask.fillColor = scene!.backgroundColor
- mask.lineWidth = 0
- crop.maskNode = mask
- ball.addChild(crop)
- let s = SKSpriteNode(color: UIColor(hue: 0.5, saturation: 0.5, brightness: 1, alpha: 1), size: CGSize(width: 80, height: 60))
- s.position = CGPoint(x: 0, y: -10)
- crop.addChild(s)
- let kingyo = SKSpriteNode(texture: SKTexture(image: img))
- kingyo.name = "kingyo"
- kingyo.position = ball.position
- scene?.addChild(kingyo)
- }
- }
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- scene?.enumerateChildNodes(withName: "kingyo", using: { (n, _) in
- let wait = TimeInterval(arc4random_uniform(10)) * 0.1
- n.run(.sequence([.wait(forDuration: wait), .move(by: CGVector(dx: 0, dy: -10), duration: 1), .move(by: CGVector(dx: 0, dy: 10), duration: 1)]))
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement