左 or 右の丸をたっちするとビヨンと拡大するiPhoneアプリのサンプルコードを描いてみます。
import UIKit
import SpriteKit
class ViewController: UIViewController, SKSceneDelegate {
weak var scene : SKScene?
weak var select : SKNode?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(hue: 0, saturation: 0.5, brightness: 1, alpha: 1)
self.setupScene()
self.createCards()
self.createBall()
}
func setupScene() {
let sv = SKView(frame: self.view.bounds)
let scene = SKScene(size: sv.frame.size)
scene.delegate = self
sv.presentScene(scene)
self.view.addSubview(sv)
self.scene = sv.scene
}
func createCards() {
let w = CGRectGetMaxX(self.view.bounds)
let h = CGRectGetMaxY(self.view.bounds)
let size = self.view.bounds.size
let card = SKSpriteNode(color: UIColor(hue: 0.4, saturation: 0.2, brightness: 0.6, alpha: 1), size: size)
card.position = CGPointMake(w/2.0, h/2.0)
self.scene?.addChild(card)
let label = SKLabelNode(text: “Which?”)
label.position = CGPointMake(0, h * 0.3);
card.addChild(label)
let a = SKShapeNode(circleOfRadius: w/3.0)
a.name = “myoption”
a.fillColor = UIColor(hue: 0.6, saturation: 0.5, brightness: 1, alpha: 1)
a.position = CGPointMake(w/2.5, 0)
card.addChild(a)
let b = SKShapeNode(circleOfRadius: w/3.0)
b.name = “myoption”
b.fillColor = UIColor(hue: 0.9, saturation: 0.5, brightness: 1, alpha: 1)
b.position = CGPointMake(–w/2.5, 0)
card.addChild(b)
}
func createBall () {
let ground = SKSpriteNode(color: UIColor.clearColor(), size: CGSize(width: CGRectGetMaxX(self.view.bounds), height: 1))
ground.position = CGPoint(x: CGRectGetMidX(self.view.bounds), y: 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size)
ground.physicsBody?.dynamic = false
self.scene?.addChild(ground)
let ball = SKShapeNode(circleOfRadius: 3)
ball.name = “ball”
ball.position = CGPoint(x: CGRectGetMidX(self.view.bounds), y: 5)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 3)
ball.physicsBody?.restitution = 0.7
ball.physicsBody?.linearDamping = 0.5
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)
if hit?.name == “myoption” {
hit?.zPosition = (hit!.parent!.children as [SKNode]).sorted({
return $0.zPosition > $1.zPosition
}).first!.zPosition + 1.0;
self.select = hit
if let ball = self.scene?.childNodeWithName(“ball”) {
ball.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 1))
}
} else {
self.select = nil
}
}
func update(currentTime: NSTimeInterval, forScene scene: SKScene) {
if let n = select {
if let ball = self.scene?.childNodeWithName(“ball”) {
let s = ball.position.y / 100 + 1
n.setScale(s)
}
}
}
}