mercredi 21 décembre 2016

next button, how to verify each background is displaying

I have this code :

class PlayScene: SKScene {
let forest = SKSpriteNode(imageNamed: "forest")
let forest2 = SKSpriteNode(imageNamed: "forest2")
let skeleton = SKSpriteNode(imageNamed: "skeleton")
let elf = SKSpriteNode(imageNamed: "elf")
let orc = SKSpriteNode(imageNamed: "orc")
//now the buttons
let attackButton = SKSpriteNode(imageNamed: "redButtonLittle")
let specialButton = SKSpriteNode(imageNamed: "purpleButtonLittle")
let healButton = SKSpriteNode(imageNamed: "greenButtonLittle")
let nextButton = SKSpriteNode(imageNamed: "nextButtonLittle")
let previousButton = SKSpriteNode(imageNamed: "previousButtonLittle")
let rand = Int(arc4random_uniform(10))

override func didMoveToView(view: SKView) {
    print("Second view")
    self.forest.position = CGPoint(x: frame.size.width, y: frame.size.height)
    forest.zPosition = 0
    addChild(self.forest)
    self.previousButton.position = CGPoint(x: 30, y: 20)
    previousButton.zPosition = 1
    addChild(self.previousButton)
    self.attackButton.position = CGPoint(x: 120, y: 20)
    attackButton.zPosition = 1
    addChild(attackButton)
    self.healButton.position = CGPoint(x: 210, y: 20)
    healButton.zPosition = 1
    addChild(self.healButton)
    self.specialButton.position = CGPoint(x: 300, y: 20)
    specialButton.zPosition = 1
    addChild(self.specialButton)
    self.nextButton.position = CGPoint(x: 390, y: 20)
    nextButton.zPosition = 1
    addChild(self.nextButton)
    self.elf.position = CGPoint(x: 390, y: 350)
    elf.zPosition = 1
    addChild(elf)
    print(rand)
    switch(rand){
    case 0..<6:
        self.skeleton.position = CGPoint(x: 60, y: 350)
        skeleton.zPosition = 1
        addChild(skeleton)
        break
    case 6..<10:
        self.orc.position = CGPoint(x: 60, y: 350)
        orc.zPosition = 1
        addChild(orc)
        break
    default:
        break
    }        
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        if self.nodeAtPoint(location) == self.previousButton{
            if {
            forest.removeFromParent()
            self.forest2.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
            forest2.zPosition = 0
                addChild(self.forest2)
            }
            else{
                forest2.removeFromParent()
                self.forest.position = CGPoint(x: frame.size.width, y: frame.size.height)
                forest.zPosition = 0
                addChild(self.forest)
            }
        }
    }
}

And I'm at the party when you touch the next button, I want it to change the background into this two forests. But I don't know how to know which one is displayed on the view. Have you any idea ? Thank You.

Aucun commentaire:

Enregistrer un commentaire