iPhoneろけっとぱんち

ちいさいロボが丸いロケットパンチをとばすiPhoneアプリのサンプルコードを描いてみます。

import UIKit

import SpriteKit

class ViewController: UIViewController, SKSceneDelegate {

    

    weak var scene : SKScene?

    weak var joint : SKPhysicsJoint?

    

    override func viewDidLoad() {

        super.viewDidLoad()

        setupScene()

        createLight()

        createRobot()

        createPunch()

        createButton()

    }

    func setupScene() {

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

        sv.presentScene(SKScene(size: sv.frame.size))

        sv.scene?.backgroundColor = UIColor(hue: 0.15, saturation: 0.3, brightness: 1, alpha: 1);

        sv.scene?.delegate = self

        sv.scene?.physicsWorld.gravity = CGVector(dx: 0, dy: –1.0)

        self.view.addSubview(sv)

        self.scene = sv.scene

    }

    

    func createLight() {

        let light = SKLightNode()

        light.position = CGPoint(x: 50, y: 50)

        light.falloff = 1.5

        light.zPosition = 1

        light.ambientColor = UIColor.whiteColor()

        light.lightColor = UIColor(white: 0.8, alpha: 1)

        light.shadowColor = UIColor.grayColor()

        light.enabled = true

        self.scene?.addChild(light)

    }

    func createRobot() {

        let robot = SKNode()

        robot.physicsBody = SKPhysicsBody(circleOfRadius: 1)

        robot.physicsBody?.dynamic = false

        robot.position = CGPoint(x: 250, y: 100)

        self.scene?.addChild(robot)

        

        let color = UIColor(hue: 0.4, saturation: 0.7, brightness: 1, alpha: 1)

        let head = SKSpriteNode(color: color, size: CGSize(width: 50, height: 30))

        head.position = CGPoint(x: 0, y: 30)

        robot.addChild(head)

        for i in 01 {

            let eye = SKSpriteNode(color: UIColor.blackColor(), size: CGSize(width: 10, height: 10))

            eye.position = CGPoint(x: i * 15, y: –5)

            head.addChild(eye)

        }

        

        let body = SKSpriteNode(color: color, size: CGSize(width: 30, height: 20))

        body.position = CGPoint(x: 0, y: 0)

        robot.addChild(body)

        

        for i in 01 {

            let foot = SKSpriteNode(color: color, size: CGSize(width: 5, height: 10))

            foot.position = CGPoint(x: i * 10, y: –20)

            robot.addChild(foot)

        }

        

        let arm = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 20, height: 8))

        arm.name = “arm”

        arm.position = CGPoint(x: 270, y: 105)

        arm.shadowCastBitMask = 0x1 << 1

        arm.lightingBitMask = 0x1 << 1

        arm.zPosition = 2

        self.scene?.addChild(arm)

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

        let pin = SKPhysicsJointPin.jointWithBodyA(robot.physicsBody, bodyB: arm.physicsBody, anchor: CGPoint(x:arm.position.x10, y:arm.position.y))

        self.scene?.physicsWorld.addJoint(pin)

    }

    

    func createPunch() {

        let arm = self.scene!.childNodeWithName(“arm”)

        let punch = SKShapeNode(circleOfRadius: 10)

        punch.name = “punch”

        punch.fillColor = UIColor.orangeColor()

        punch.position = arm!.convertPoint(CGPoint(x: 15, y: 0), toNode: self.scene!)

        punch.physicsBody = SKPhysicsBody(circleOfRadius: 10)

        punch.physicsBody?.density = 0.1

        self.scene?.addChild(punch)

        

        let pin = SKPhysicsJointPin.jointWithBodyA(punch.physicsBody, bodyB: arm!.physicsBody, anchor: punch.position)

        self.scene?.physicsWorld.addJoint(pin)

        self.joint = pin

    }

    

    func createButton() {

        let btn = SKShapeNode(circleOfRadius: 80)

        btn.name = “btn”

        btn.position = CGPoint(x: CGRectGetMaxX(self.view.bounds), y: 0)

        btn.fillColor = UIColor.orangeColor()

        btn.strokeColor = UIColor.darkGrayColor()

        btn.lineWidth = 2

        self.scene?.addChild(btn)

    }

    

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

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

        

        if let hit = self.scene?.nodeAtPoint(p) {

            if hit.name == “btn” {

                let punch = self.scene?.childNodeWithName(“punch”)?.physicsBody

                if let pjoint = self.joint {

                    self.scene?.physicsWorld.removeJoint(pjoint)

                    createPunch()

                }

            }

        }

    }

    

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

        let arm = self.scene?.childNodeWithName(“arm”)

        arm?.physicsBody?.angularVelocity = –10

        for p in self.scene!.children {

            if p.position.y < 0 {

                p.removeFromParent()

            }

        }

    }

    

}