iPhoneバッテン上下左右

バツ印を上下左右に交差させるiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SpriteKit

class ViewController: UIViewController, SKSceneDelegate {

    weak var scene : SKScene?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createPlusMark()

    }

    func setupScene() {

        scene = {

            let sv = SKView(frame: self.view.bounds)

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

            s.delegate = self

            s.backgroundColor = UIColor(hue: 0.12, saturation: 0.5, brightness: 1, alpha: 1)

            sv.presentScene(s)

            self.view.addSubview(sv)

            return s

        }()

    }

    func createPlusMark() {

        [Int](099).foreach { i in

            let plus = SKNode()

            plus.position = CGPoint( x: CGFloat(i%10) * 60 + 10, y: CGFloat(i/10) * 60 + 50)

            plus.addChild(SKSpriteNode(color: UIColor.grayColor(), size: CGSizeMake(50, 5)))

            plus.addChild(SKSpriteNode(color: UIColor.grayColor(), size: CGSizeMake(5, 50)))

            plus.name = “mark \(i)

            self.scene?.addChild(plus)

        }

    }

    

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

        scene.children

            .filter { ($0 as SKNode).name != nil }

            .filter { ($0 as SKNode).name?.componentsSeparatedByString(” “)[0] == “mark”}

            .foreach { n in

                let num = n.name!.componentsSeparatedByString(” “)[1].toInt()!

                switch num % 4 {

                case 0:

                    let x = n.position.x + 1

                    (n as SKNode).position = (x < 585)

                        ? CGPoint(x: x, y: n.position.y)

                        : CGPoint(x: 10, y: n.position.y)

                    break;

                case 1:

                    let y = n.position.y + 1

                    (n as SKNode).position = (y < 645)

                        ? CGPoint(x: n.position.x, y: y)

                        : CGPoint(x: n.position.x, y: 0)

                    break;

                case 2:

                    let x = n.position.x 1

                    (n as SKNode).position = (x > 10)

                        ? CGPoint(x: x, y: n.position.y)

                        : CGPoint(x: 585, y: n.position.y)

                    break;

                case 3:

                    let y = n.position.y 1

                    (n as SKNode).position = (y > 0)

                        ? CGPoint(x: n.position.x, y: y)

                        : CGPoint(x: n.position.x, y: 645)

                    break;

                default:

                    break;

                }

        }

    }

    

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

        let p = touches.anyObject()!.locationInNode(scene)

        scene?.nodesAtPoint(p).foreach { i in

            i.runAction(SKAction.rotateByAngle(CGFloat(M_PI) * 2.0, duration: 2.0))

        }

    }

}

extension Array {

    func foreach(doit:T -> Void) {for i in self {doit(i)}}

}