iPhone棒バランス

立てた棒のバランスで遊ぶ、iPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SpriteKit

class ViewController: UIViewController, SKSceneDelegate {

    weak var scene : SKScene?

    weak var touch : UITouch?

    var direction = “”

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createSlideBoard()

        createStick()

    }

    

    func setupScene() {

        let sv = SKView(frame: view.bounds)

        let s = SKScene(size: sv.frame.size)

        s.delegate = self

        s.backgroundColor = UIColor(hue: 0.75, saturation: 0.2, brightness: 1, alpha: 1)

        sv.presentScene(s)

        view.addSubview(sv)

        self.scene = s

    }

    

    func createSlideBoard() {

        let board = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(100, 30))

        board.name = “board”

        board.position = CGPoint(x: CGRectGetMidX(view.bounds), y: CGRectGetMidY(view.bounds) 150)

        self.scene?.addChild(board)

        

        [Int](01).each {(i, e) -> Void in

            let l = SKLabelNode(text: “▲”)

            l.fontColor = UIColor.whiteColor()

            l.fontSize = 18

            l.position = CGPointMake(40 80 * CGFloat(i), 0)

            l.zRotation = i == 0 ? CGFloat(M_PI) / 6.0 :  1 * CGFloat(M_PI) / 6.0

            board.addChild(l)

        }

        

        board.physicsBody = SKPhysicsBody(rectangleOfSize: board.size)

        board.physicsBody?.dynamic = true

        board.physicsBody?.friction = 0.9

        

        let anchor = SKSpriteNode(color: UIColor.clearColor(), size: CGSizeMake(1, 1))

        anchor.physicsBody = SKPhysicsBody(rectangleOfSize: anchor.size)

        anchor.physicsBody?.dynamic = false

        scene?.addChild(anchor)

        

        let joint = SKPhysicsJointSliding.jointWithBodyA(anchor.physicsBody, bodyB: board.physicsBody, anchor: board.position, axis: CGVectorMake(1, 0))

        scene?.physicsWorld.addJoint(joint)

        

    }

    

    func createStick() {

        let stick = SKShapeNode(rectOfSize: CGSizeMake(10, 200), cornerRadius: 2)

        stick.fillColor = UIColor.brownColor()

        stick.position = CGPoint(x: CGRectGetMidX(view.bounds), y: 400)

        stick.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(8, 200))

        stick.physicsBody?.friction = 1.0

        stick.physicsBody?.mass = 0.1

        stick.physicsBody?.linearDamping = 1.0

        scene?.addChild(stick)

    }

    

    

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

        touch = touches.anyObject() as? UITouch

    }

    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

        touch = touches.anyObject() as? UITouch

    }

    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {

        touch = nil

    }

    

    

    func update(currentTime: NSTimeInterval, forScene scene: SKScene) {

        let board = scene.childNodeWithName(“board”)

        if let touch = self.touch {

            let p = touch.locationInNode(scene)

            if board?.position.x < p.x {

                if direction != “R” { board?.physicsBody?.velocity = CGVector(dx: 0, dy: 0) }

                direction = “R”

                board?.physicsBody?.applyImpulse(CGVector(dx: 5, dy: 0))

            } else if board?.position.x > p.x {

                if direction != “L” { board?.physicsBody?.velocity = CGVector(dx: 0, dy: 0) }

                direction = “L”

                board?.physicsBody?.applyImpulse(CGVector(dx: –5, dy: 0))

            }

        } else {

            board?.physicsBody?.velocity = CGVector(dx: 0, dy: 0)

        }

    }

}

extension Array {

    func each(doit:(Int, T) -> Void) { for (i, e) in enumerate(self) { doit(i, e) } }

}