vendredi 24 juillet 2015

Collision checking issues

The code checks if the user taps a ball with the same texture as a ball that randomly changes textures. But in my console "Point" is only printed occasionally although I tap on the ball with the same texture as the ball that randomly changes texture. How can this be fixed. Has this something to do with adding a physicsBody to the other balls?

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

    var array = [blueTexture, redTexture, greenTexture, yellowTexture]// var with value of textures

    var blackB = childNodeWithName("changeBall") as! SKSpriteNode

    let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
    blackB.texture = array[randomIndex] // blackB is given a random texture 

    for t in touches { // check each touch

        let touch = t as! UITouch
        let pos = touch.locationInNode(self) // find touch position

        for child in self.children { // check each children in scene
            if let ball = child as? SKSpriteNode{ 
                if ball !== blackB && ball.containsPoint(pos) { // check for collision, but skip if it's a blackB
                    if ball.texture == blackB.texture { // collision found, check color
                        println("POINT")
                    }
                }
            }
        }
    }


}

Aucun commentaire:

Enregistrer un commentaire