Skip to content

Instantly share code, notes, and snippets.

@mzsima
Created October 12, 2017 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzsima/0c774b164ae20bf7f7cafb20f542246e to your computer and use it in GitHub Desktop.
Save mzsima/0c774b164ae20bf7f7cafb20f542246e to your computer and use it in GitHub Desktop.
missing circle
//
// ViewController.swift
// MissingCircle
//
// Created by MizushimaYusuke on 2017/10/12.
// Copyright © 2017 MizushimaYusuke. All rights reserved.
//
import UIKit
import SpriteKit
class ViewController: UIViewController {
weak var scene: SKScene?
override func viewDidLoad() {
super.viewDidLoad()
setupScene()
createWall()
}
func setupScene() {
let sv = SKView(frame: view.bounds)
sv.showsPhysics = true
let s = SKScene(size: sv.frame.size)
sv.presentScene(s)
view.addSubview(sv)
scene = s
}
func createWall() {
let wall = SKSpriteNode(color: .clear, size: view.frame.size)
wall.position = view.center
scene?.addChild(wall)
wall.physicsBody = SKPhysicsBody(edgeLoopFrom: CGRect(origin: CGPoint(x: -view.center.x, y: -view.center.y), size: wall.frame.size))
wall.physicsBody?.isDynamic = false
}
func createMissingCircle() -> SKNode {
let hue = CGFloat(arc4random_uniform(10)) * 0.1
let color = UIColor(hue: hue, saturation: 0.7, brightness: 1, alpha: 1)
let s = CGFloat(arc4random_uniform(30)) + 30
UIGraphicsBeginImageContextWithOptions(CGSize(width: s, height: s), false, 0)
if let ctx = UIGraphicsGetCurrentContext() {
color.set()
ctx.fillEllipse(in: CGRect(x: 0, y: 0, width: s, height: s))
ctx.setBlendMode(.clear)
ctx.fillEllipse(in: CGRect(x: s * 0.2, y: s * 0.8, width: s, height: s))
}
let img = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
let texture = SKTexture(image: img)
let n = SKSpriteNode(texture: texture)
n.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
self.scene?.addChild(n)
return n
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
let p = touch.location(in: scene!)
let n = createMissingCircle()
n.position = p
}
}
}
@mzsima
Copy link
Author

mzsima commented Oct 12, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment