jeudi 23 juillet 2015

Swift Comparing two color objects to see if their color is the same with touch

The choiceBall is set to black but randomly changes color to match one of the other balls with each tap. The idea is that I tap the the other balls according to what the choiceBall's color is.

For Example: The choiceBall turns blue. I touch the blue ball. The choiceBall turns red I touch the red ball etc.

How can I check if I have tapped the right ball according to the choiceBall.

Thanks for any help :)

class GameScene: SKScene {

   var choiceBall = SKShapeNode(circleOfRadius: 50)
   var blueBall = SKShapeNode(circleOfRadius: 50)
   var greenBall = SKShapeNode(circleOfRadius: 50)
   var yellowBall = SKShapeNode(circleOfRadius: 50)
   var redBall = SKShapeNode(circleOfRadius: 50)

   var array = [SKColor(red: 0.62, green: 0.07, blue: 0.04, alpha: 1), SKColor(red: 0, green: 0.6, blue: 0.8, alpha: 1), SKColor(red: 0, green: 0.69, blue: 0.1, alpha: 1), SKColor(red: 0.93, green: 0.93, blue: 0, alpha: 1)]

 override func didMoveToView(view: SKView) {

    choiceBall.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
    choiceBall.fillColor = SKColor.blackColor()
    self.addChild(choiceBall)

    blueBall.position = CGPointMake(self.frame.size.width * 0.35, self.frame.size.height * 0.25)
    blueBall.fillColor = SKColor(red: 0, green: 0.6, blue: 0.8, alpha: 1)
    self.addChild(blueBall)

    redBall.position = CGPointMake(self.frame.size.width * 0.65, self.frame.size.height * 0.75)
    redBall.fillColor = SKColor(red: 0.62, green: 0.07, blue: 0.04, alpha: 1)
    self.addChild(redBall)

    yellowBall.position = CGPointMake(self.frame.size.width * 0.35, self.frame.size.height * 0.75)
    yellowBall.fillColor = SKColor(red: 0.93, green: 0.93, blue: 0, alpha: 1)
    self.addChild(yellowBall)

    greenBall.position = CGPointMake(self.frame.size.width * 0.65, self.frame.size.height * 0.25)
    greenBall.fillColor = SKColor(red: 0, green: 0.69, blue: 0.1, alpha: 1)
    self.addChild(greenBall)
}

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

    let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
    choiceBall.fillColor = array[randomIndex]
}

Aucun commentaire:

Enregistrer un commentaire