对于我正在创建的游戏,SKSpriteNode逐渐沿着用户的屏幕向下移动。在屏幕底部附近有另一个SKSpriteNode(位置是静态的),只留下一点空间让原始SKSpriteNode适合。我需要检测第一个SKSpriteNode何时完全低于第二个SKSpriteNode,我有有点麻烦。这是我目前正在使用的代码:
if (pos.y > barPosY) //pos.y = 1st SKSpriteNode, barPosY = 2nd SKSpriteNode
{
touchedTooEarly = true
}
由于某种原因,当第一个SKSpriteNode稍微超过第二个SKSpriteNode(不是一直)时,它仍然会检测到它完全结束。是否存在我缺少的坐标空间问题?
由于
答案 0 :(得分:2)
精灵a
包含精灵b
,如果
b.frame
位于a.frame
b.zPosition
低于a.zPosition
现在让我们构建一个扩展
extension SKSpriteNode {
func isCoveredBy(otherSprite: SKSpriteNode) -> Bool {
let otherFrame = CGRect(
origin: convertPoint(otherSprite.position, fromNode: otherSprite),
size: otherSprite.frame.size
)
return zPosition < otherSprite.zPosition && CGRectContainsRect(frame, otherFrame)
}
}
这种机制完全忽略了透明度。
如果比较相同类型的2个精灵,则函数CGRectContainsRect
仅在精确对齐时才会返回true。虽然你可以解决这个问题,比较精灵时创建一个较小的矩形。