mardi 4 août 2015

how to make my win label stay hidden until the user win?

am trying to make my label hidden by changing the x and y parameter but when i start playing the game and on the first click suddenly the label appear i tried the alpha way and all the other way to hide a label were fine except this one.

this my code:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var imageView: UIImageView!


    var goNumber = 1

    var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0]

    let winnerCombination = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]]

    var winner = 0


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


  override func viewDidAppear(animated: Bool) {
        self.label.center = CGPointMake(self.label.center.x - 400, self.label.center.y)

    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
        @IBAction func buttonPressed(sender: UIButton) {

        if gameState[sender.tag] == 0 {
            var image = UIImage(named: "")
            if goNumber % 2 == 0 {
                image = UIImage(named: "cross.png")
                gameState[sender.tag] = 1
            }
            else{
                image = UIImage(named: "nought.png")
                gameState[sender.tag] = 2
            }
            for combination in winnerCombination {

                if (gameState[combination[0]] == gameState[combination[1]] && gameState[combination[0]] == gameState[combination[2]] && gameState[combination[0]] != 0) {

                    winner = gameState[combination[0]]

                    println("the winner is \(winner)")
                }
            }
            if winner != 0 {

                if winner == 1 {
                    label.text = "cross won"
                }
                else {
                    label.text = "nought won"
                }
                UIView.animateWithDuration(0.5, animations: {
                    self.label.center = CGPointMake(self.label.center.x + 400, self.label.center.y)

                    self.label.alpha = 1
                })
            }
            sender.setImage(image, forState: .Normal)
            goNumber++
        }
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire