Nimbahus
Nimbahus

Reputation: 487

Swift Sprite Kit passing Bool

I got a Image with 2 faces, Up and Down ( Imagine a Card ). Whenever you tap the Image, it changes it image.

I used a Bool to get the expression. However, after the Scene is presented again, I want to show the image which was Up/Down in the last Scene. Note that this is only 1 Scene, and it presents itself again.

How can I pass a Bool value between the Scene?

//Update:

My Code:

....
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)

}

Upvotes: 0

Views: 98

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

You can define a function in GameScene like the following.

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

And call it from didMoveToView in the new scene.

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.

}

Also set the old FaceUp value in the new scene in 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)

}

Upvotes: 1

Related Questions