Collision checking issues

你说的曾经没有我的故事 提交于 2019-12-25 06:05:31

问题


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")
                    }
                }
            }
        }
    }


}

回答1:


Some observations...

  1. nodeAtPoint(pos) can replace the for loop over child nodes
  2. ball.containsPoint(pos) is not needed because nodeAtPoint performs the same test
  3. ball !== blackB should be ball != blackB
  4. Perhaps changing to a random texture after checking for a texture match makes more sense

An implementation with the above changes

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

    var array = [blueTexture, redTexture, greenTexture, yellowTexture]
    var blackB = childNodeWithName("changeBall") as! SKSpriteNode

    if let touch = touches.first as? UITouch {
        let pos = touch.locationInNode(self)
        if let ball = nodeAtPoint(pos) as? SKSpriteNode {
            if ball != blackB && ball.texture == blackB.texture {
                println("POINT")
            }
        }
    }
    // Change texture after checking for a texture match
    let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
    blackB.texture = array[randomIndex]
}



回答2:


I assume you wish that something must happen when you touch the same textured ball...

if ball.texture == blackB.texture { 
  println("POINT"); //This is the only instruction read by the computer and nothing else
}

if ball.texture == blackB.texture {
  println("POINT");
  doSomething();//Insert expected function like here
}

UPDATE

if(ball.texture == blackB.texture){
  println("POINT");
  score+=1;
  label.text = String(score); // Update the label.text value everytime score changes
}


来源:https://stackoverflow.com/questions/31614021/collision-checking-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!