LoginSignup
6
6

More than 5 years have passed since last update.

Swift倉庫・SKSceneでiOSタップとOSXマウス統一

Last updated at Posted at 2014-07-29

【iOSタップ・OSXクリック統一】

iOS側とOSX側のSKSceneにオーバーライドを行い、タップとマウスクリックの出力を拡張ファンクションに飛ぶようにする事で、様々なSKSceneでiOSとOSX対応のものが作れます。

CScene.swift
import SpriteKit

#if os(iOS)
    import UIKit
#endif

class CScene: SKScene {

    #if os(iOS) // iOS SKSceneオーバーライド

    // [ iOS ] : Touches Began
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let previousLocation = touch.previousLocationInNode(self)
            ctouchesBegan(location, previousLocation: previousLocation)
        }
    }

    // [ iOS ] : Touches Moved
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let previousLocation = touch.previousLocationInNode(self)
            ctouchesMoved(location, previousLocation: previousLocation)
        }
    }

    // [ iOS ] : Touches Ended
    override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let previousLocation = touch.previousLocationInNode(self)
            ctouchesEnded(location, previousLocation: previousLocation)
        }
    }

    // [ iOS ] : Touches Cancelled
    override func touchesCancelled(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let previousLocation = touch.previousLocationInNode(self)
            ctouchesCancelled(location, previousLocation: previousLocation)
        }
    }

    #else   // OSX SKSceneオーバーライド

    // [ OSX ] : Mouse Down
    override func mouseDown(theEvent: NSEvent) {
        let location = theEvent.locationInNode(self)
        let previousLocation = location
        ctouchesBegan(location, previousLocation: previousLocation)
    }

    // [ OSX ] : Mouse Moved
    override func mouseMoved(theEvent: NSEvent) {
        let location = theEvent.locationInNode(self)
        let previousLocation = location
        ctouchesMoved(location, previousLocation: previousLocation)
    }

    // [ OSX ] : Mouse Dragged
    override func mouseDragged(theEvent: NSEvent) {
        let location = theEvent.locationInNode(self)
        let previousLocation = location
        ctouchesMoved(location, previousLocation: previousLocation)
    }

    // [ OSX ] : Mouse Up
    override func mouseUp(theEvent: NSEvent) {
        let location = theEvent.locationInNode(self)
        let previousLocation = location
        ctouchesEnded(location, previousLocation: previousLocation)
    }

    #endif

    // SKScene統一出力先

    // [ OSX/iOS ] : Touches Began
    func ctouchesBegan(touchLocation: CGPoint, previousLocation: CGPoint) {
    }

    // [ OSX/iOS ] : Touches Moved
    func ctouchesMoved(touchLocation: CGPoint, previousLocation: CGPoint) {

    }

    // [ OSX/iOS ] : Touches Ended
    func ctouchesEnded(touchLocation: CGPoint, previousLocation: CGPoint) {

    }

    // [ OSX/iOS ] : Touches Cancelled
    func ctouchesCancelled(touchLocation: CGPoint, previousLocation: CGPoint) {

    }

}

そして、下記のようにひとつのCScene(SKScene)でiOSとOSXからの入力を受ける事が出来ます。

使用例:CSampleScene.swift
import SpriteKit

class CSampleScene: CScene {

    var centerPoint: CGPoint = CGPointMake(0,0)

    var theSprite = SKSpriteNode()

    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        self.centerPoint = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame))

        let myLabel = SKLabelNode(fontNamed:"Chalkduster")
        myLabel.text = "Hello, World!";
        myLabel.fontSize = 24;
        myLabel.position = self.centerPoint
        addChild(myLabel)

        theSprite = SKSpriteNode(imageNamed:"Spaceship")
        theSprite.position = self.centerPoint
        theSprite.zPosition = 10
        addChild(theSprite)

    }

    override func ctouchesBegan(touchLocation: CGPoint, previousLocation: CGPoint) {
        println("CTouches Began!!")
    }

    override func ctouchesMoved(touchLocation: CGPoint, previousLocation: CGPoint) {
        println("CTouches Moved!!")
    }

    override func ctouchesEnded(touchLocation: CGPoint, previousLocation: CGPoint) {
        println("CTouches Ended!!")
    }

    override func ctouchesCancelled(touchLocation: CGPoint, previousLocation: CGPoint) {
        println("CTouches Cancelled!!")
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }

}

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6