이 코드는 사용자가 텍스처를 임의로 변경하는 공과 같은 텍스처의 공을 탭했는지 확인합니다. 그러나 내 콘솔에서 "Point"는 무작위로 질감을 바꾸는 공과 같은 질감의 공을 두드려도 가끔 인쇄됩니다. 이 문제를 어떻게 해결할 수 있습니까? 다른 공에 physicsBody를 추가하는 것과 관련이 있습니까?
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")
}
}
}
}
}
}
일부 관찰 ...
nodeAtPoint(pos)
for
자식 노드 의 루프를 대체 할 수 있습니다.ball.containsPoint(pos)
nodeAtPoint
동일한 테스트를 수행 하므로 필요하지 않습니다.ball !== blackB
해야한다 ball != blackB
위의 변경 사항이 적용된 구현
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]
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다