gpt4 book ai didi

ios - SKPhysicsBody 一侧碰撞检测

转载 作者:行者123 更新时间:2023-11-29 03:08:24 26 4
gpt4 key购买 nike

是否可以检测节点是否仅在一侧与另一侧发生碰撞?

例如,一只动物与另一只动物发生冲突。在这种情况下,碰撞检测方法被调用。但是当动物跳过动物并在其上“行走”时,碰撞检测不会被激活。

我将不胜感激每一个答案。谢谢

我的代码:

if (lionNode.frame.origin.y + lionNode.frame.size.height < animalNode.frame.origin.y) {
NSLog(@"Walk");
}
else {
NSLog(@"dead");
}

最佳答案

在碰撞检测回调中检查以下内容。

if(y-origin of the first animal + its height < the y-origin of the second animal){
//then do the logic for walking on top of another animal (probably nothing would happen here depending on what you are trying to build)
}
else {
//forward to collision logic (e.g life loss, lose points, restart level ,etc..)
}

如果第一只动物有可能进入第二只动物的下方,那么您需要将条件更新为以下内容

if(firstAnimal.origin.y + firstAnimal.frame.size.height < secondAnimal.frame.origin.y  
|| firstAnimal.origin.y > secondAnimal.frame.origin.y + secondAnimal.frame.size.height)

当心不同的坐标系,它有时会造成混淆。希望这对您有所帮助,如有任何不清楚的地方,请随时询问。

关于ios - SKPhysicsBody 一侧碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22511490/

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