Reputation: 147
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. So that I can implement a score board to keep score.
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]
}
Upvotes: 0
Views: 831
Reputation: 332
Basically you need to check all touches with collisions with all balls and if they collide, you can check colors. Don't forget to skip choice ball if you don't want it to be checked for taps as well.
for t in touches { // check each touch
let touch = t as! UITouch // convert to UITouch for pre Swift 2.0
let pos = touch.locationInNode(self) // find touch position
for child in self.children { // check each children in scene
if let ball = child as? SKShapeNode { // convert child to the shape node
if ball !== choiceBall && ball.containsPoint(pos) { // check for collision, but skip if it's a choice ball
if ball.fillColor == choiceBall.fillColor { // collision found, check color
// do your stuff, colors match
}
}
}
}
}
Upvotes: 2