gpt4 book ai didi

ios - 检测 SKSpriteNode 何时完全低于另一个 SKSpriteNode

转载 作者:可可西里 更新时间:2023-11-01 01:35:34 25 4
gpt4 key购买 nike

对于我正在创建的游戏,SKSpriteNode 逐渐向下移动到用户屏幕上。屏幕底部附近还有另一个 SKSpriteNode(位置是静态的),只为原始 SKSpriteNode 留出一点空间。我需要检测第一个 SKSpriteNode 何时完全低于第二个 SKSpriteNode,我有有点麻烦。这是我目前使用的代码:

if (pos.y > barPosY) //pos.y = 1st SKSpriteNode, barPosY = 2nd SKSpriteNode
{
touchedTooEarly = true
}

出于某种原因,当第一个 SKSpriteNode 稍微超过第二个 SKSpriteNode 时(不是一直),它仍然检测到它完全结束。是否存在我遗漏的坐标空间问题?

最佳答案

逻辑

一个 Sprite a覆盖了一个 Sprite b如果

  1. b.framea.frame
  2. 里面
  3. 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)
}
}

问题 #1:透明度

这种机制完全忽略了透明度。

问题#2:相同的 Sprite

如果您比较 2 个相同类型的 Sprite ,函数 CGRectContainsRect 将仅在它们完全对齐时返回 true。虽然您可以在比较 Sprite 时创建一个较小的矩形来解决这个问题。

关于ios - 检测 SKSpriteNode 何时完全低于另一个 SKSpriteNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38127974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com