我有一个游戏,那里有一个绿色的圆圈,随机地在场景中的任意位置产卵,每次点击这个圆圈,它就会改变位置,稍微有可能变成红色。当圆圈是红色的,我希望用户点击离开屏幕房地产,而不是红色的圆圈。我怎么才能探测到不是在圆圈上的抽头?我的圆圈是SKShapeNode。我处理与touchesBegan函数的接触。
发布于 2015-12-09 16:43:42
要确定用户的触摸是在圆的内部还是外部,1)计算触点与圆心之间的距离,2)比较该距离是否小于或等于圆的半径。下面是一个如何做到这一点的例子:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
let dx = location.x - circle.position.x
let dy = location.y - circle.position.y
let distance = sqrt(dx*dx + dy*dy)
if (distance <= CGFloat(radius)) {
print ("inside of circle")
}
else {
print ("outside of circle")
}
}
}https://stackoverflow.com/questions/34169839
复制相似问题