如何添加转到不同场景的多个按钮?

时间:2014-08-09 10:06:10

标签: ios swift xcode sprite-kit

有人可以告诉我代码有多个按钮可以进入不同的场景,我正在使用Xcode 6 Swift。

这是我目前的代码,它完美无缺但我想让按钮图像'得分'转到另一个场景,有人可以告诉我如何。

import SpriteKit

class GameScene: SKScene {

    let playbutton = SKSpriteNode (imageNamed: "play")
    let score = SKSpriteNode (imageNamed: "score")

    override func didMoveToView(view: SKView) {
        self.playbutton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        self.addChild(self.playbutton)
        self.backgroundColor  = UIColor(hex: 0x80D9FF)

        self.score.position = CGPointMake(CGRectGetMidX(self.frame), 100)
        self.addChild(self.score)
        self.backgroundColor  = UIColor(hex: 0x80D9FF)
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            if self.nodeAtPoint(location) == self.playbutton {
                var scene = PlayScene(size: self.size)
                let skView = self.view as SKView
                skView.ignoresSiblingOrder = true
                scene.scaleMode = .ResizeFill
                scene.size = skView.bounds.size
                skView.presentScene(scene)
            }
        }
    }
}

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

1 个答案:

答案 0 :(得分:0)

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let skView = self.view as SKView
        skView.ignoresSiblingOrder = true
        if self.nodeAtPoint(location) == self.playbutton {
            var scene = PlayScene(size: self.size)
            scene.scaleMode = .ResizeFill
            scene.size = skView.bounds.size
            skView.presentScene(scene)
        } else if self.nodeAtPoint(location) == self.score {
            var scene = HighScore(size: self.size)
            scene.scaleMode = .ResizeFill
            scene.size = skView.bounds.size
            skView.presentScene(scene)
        }
    }
}