通过Bool的Swift Sprite Kit

时间:2015-03-16 17:37:14

标签: ios swift sprite-kit

我有一张有两张脸的图片,上下(想象一张卡片)。 每当您点击图像时,它都会更改图像。

我使用Bool来获取表达式。但是,在再次呈现场景之后,我想在最后一个场景中显示向上/向下的图像。请注意,这只是一个场景,它再次出现。

如何在场景之间传递Bool值?

//更新:

我的代码:

....
var FaceUp = true
.... 

override func didMoveToView(view: SKView) {

TTexture = SKTexture(imageNamed: "TCard.png")
TTexture.filteringMode = .Nearest
T2Texture = SKTexture(imageNamed: "T2Card.png")
T2Texture.filteringMode = .Nearest

Card = SKSpriteNode(texture: TTexture)
Card.position = CGPoint(x: Rate.position.x - (TransparentLayer.size.width / 3), y: Rate.position.y)
Card.size = CGSize(width: 50, height: 50)
Card.name = "Card"
Card.zPosition = 100
self.addChild(Card)

flip(FaceUp)

}

func flip(state : Bool) {
    FaceUp = state
    if (!state) {
        Card.texture = TTexture //Error
        Player.texture = T1
        Player.runAction(RunAnimation1)
        FaceUp = true
    } else {
        Card.texture = T2Texture //Error
        Player.texture = T2
        Player.runAction(RunAnimation2)
        FaceUp = false
    }
}

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

    let touch:UITouch = touches.anyObject() as UITouch
    let touchLocation = touch.locationInNode(self)

    let Node:SKNode = self.nodeAtPoint(touchLocation)

    if Node.name == "Card" {

        let Scale1 = SKAction.scaleTo(0.7, duration: 0.1)
        let Scale2 = SKAction.scaleTo(1, duration: 0.1)
        let Scaling = SKAction.sequence([Scale1, Scale2])
        Card.runAction(Scaling)

        self.flip(FaceUp)

}
}

func Restart(){

    self.saveHighScore()

    LastScore = Score
    self.addLeaderboardScore(self.Score)
    let Transition = SKTransition.fadeWithDuration(1.8)
    let Scene = GameScene(size: self.frame.size)
    Scene.LastScore = Score
    Scene.FaceUp = FaceUp
    scene?.scaleMode = SKSceneScaleMode.AspectFill

    self.scene?.view?.presentScene(Scene, transition: Transition)

}

1 个答案:

答案 0 :(得分:1)

您可以在GameScene中定义一个函数,如下所示。

func setCardState(state : Bool) {
    FaceUp = state
    if (!state) {
        Card.texture = Texture2
        Player.texture = T2
    } else {
        Card.texture = Texture1
        Player.texture = T1
    }
}

并在新场景中从didMoveToView调用它。

override func didMoveToView(view: SKView) {

    TTexture = SKTexture(imageNamed: "TCard.png")
    TTexture.filteringMode = .Nearest
    T2Texture = SKTexture(imageNamed: "T2Card.png")
    T2Texture.filteringMode = .Nearest

    Card = SKSpriteNode(texture: TTexture)
    Card.position = CGPoint(x: Rate.position.x -  (TransparentLayer.size.width / 3), y: Rate.position.y)
    Card.size = CGSize(width: 50, height: 50)
    Card.name = "Card"
    Card.zPosition = 100
    self.addChild(Card)

    setCardState(FaceUp) // added line.

}

还在FaceUp中的新场景中设置旧的Restart function值。

func Restart(){

    self.saveHighScore()

    LastScore = Score
    self.addLeaderboardScore(self.Score)
    let Transition = SKTransition.fadeWithDuration(1.8)
    let Scene = GameScene(size: self.frame.size)
    Scene.LastScore = Score
    scene?.scaleMode = SKSceneScaleMode.AspectFill

    Scene.FaceUp = FaceUp //changed line.  

    self.scene?.view?.presentScene(Scene, transition: Transition)

}