
扇を開いてボールを箱に入れていくiPhoneアプリのサンプルコードを描いてみます。
import UIKit
import SpriteKit
class ViewController: UIViewController {
weak var scene : SKScene?
override func viewDidLoad() {
super.viewDidLoad()
self.setupScene()
self.createFans()
self.createBox()
self.createBall()
}
func setupScene() {
let sv = SKView(frame: self.view.bounds)
let scene = SKScene(size: sv.frame.size)
scene.backgroundColor = UIColor(hue: 0, saturation: 0.3, brightness: 0.8, alpha: 1)
sv.presentScene(scene)
self.view.addSubview(sv)
self.scene = scene
}
func createFans() {
for j in 0…3 {
let fan = SKNode()
fan.name = “fan”
fan.position = CGPoint(x: 90 * j + 60, y: 200 + j * 50)
self.scene?.addChild(fan)
for i in 0…9 {
let size = CGSize(width: 16, height: 80)
let f = SKShapeNode(rect: CGRect(origin: CGPoint(x: –8, y: 0), size:size))
f.name = “fan\(i)“
f.fillColor = UIColor(hue: 0.3, saturation: 0.4, brightness: 1, alpha: 1)
f.physicsBody = SKPhysicsBody(rectangleOfSize: size, center: CGPoint(x: 0, y: 40))
f.physicsBody?.dynamic = false
f.physicsBody?.categoryBitMask = 0x1 << 1
f.physicsBody?.collisionBitMask = 0x1 << 2
f.physicsBody?.pinned = true
fan.addChild(f);
}
let btn = SKShapeNode(circleOfRadius: 15)
btn.name = “btn”
btn.fillColor = UIColor(hue: 0.6, saturation: 0.4, brightness: 1, alpha: 1)
fan.addChild(btn)
}
}
func createBox() {
let bar = SKSpriteNode(color: UIColor(hue: 0.5, saturation: 0.5, brightness: 1, alpha: 1), size: CGSizeMake(CGRectGetMaxX(self.view.bounds), 10))
bar.position = CGPoint(x: CGRectGetMidX(self.view.bounds), y: 5)
bar.physicsBody = SKPhysicsBody(rectangleOfSize: bar.size)
bar.physicsBody?.dynamic = false
self.scene?.addChild(bar)
for i in 0…5 {
let sbar = SKSpriteNode(color: bar.color, size: CGSizeMake(10, 50))
sbar.position = CGPoint(x: CGFloat(i) * CGRectGetMaxX(self.view.bounds) / 5.0, y: 30)
sbar.physicsBody = SKPhysicsBody(rectangleOfSize: sbar.size)
sbar.physicsBody?.dynamic = false
self.scene?.addChild(sbar)
}
}
func createBall() {
let ball = SKShapeNode(circleOfRadius: 20)
ball.name = “ball”
ball.fillColor = UIColor(hue: 0.8, saturation: 0.3, brightness: 1, alpha: 1)
ball.position = CGPoint(x: CGRectGetMaxX(self.view.bounds) – 100, y: CGRectGetMaxY(self.view.bounds) * 0.8)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 20)
ball.physicsBody?.linearDamping = 0.8
ball.physicsBody?.restitution = 0.2
ball.physicsBody?.dynamic = false
self.scene?.addChild(ball)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let p = touches.anyObject()!.locationInNode(self.scene)
let hit = self.scene?.nodeAtPoint(p)
var cnt : CGFloat = 0.0
if hit?.name == “btn” {
hit?.parent?.enumerateChildNodesWithName(“fan[0-9]”, usingBlock: { (node, finish) -> Void in
if node.zRotation == 0 {
node.zRotation = cnt * 0.2 – 1.0;
} else {
node.zRotation = 0
}
++cnt
})
} else if hit?.name == “ball” {
hit?.physicsBody?.dynamic = true
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(1.0 * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), {
self.createBall()
})
}
}
}