spriteKit中联系人的双重通知 [英] Double notification on contact in spriteKit
问题描述
当我的 SKSpriteNode 点击与 worldCategory 联系时,我似乎收到了双重通知,这是怎么回事?当我想在它触及 worldCategory 时运行一个动作时,这会产生问题,因为该动作正在被触发
I seem to get a double notification when my SKSpriteNode hits has contact with the worldCategory, how come is this? This creates problem when i want to run an action when it touches the worldCategory, since the action is being triggered
这是我在 InitWithSize 方法中的位掩码
Here is my bitmask in the InitWithSize method
mover.physicsBody.categoryBitMask = birdCategory;
bottom.physicsBody.categoryBitMask = worldCategory;
mover.physicsBody.contactTestBitMask = worldCategory;
这里是联系方式:
- (void)didBeginContact:(SKPhysicsContact *)contact {
if (contact.bodyA.categoryBitMask == worldCategory) {
mover.texture = [SKTexture textureWithImageNamed:@"birddead1"];
NSLog(@"Contact");
self.scene.paused = YES;
[pauseButton removeFromSuperview];
}
}
在我的日志中显示了两行联系人"
In my log there is being shown two lines with "Contact"
推荐答案
将对象的 restitution
属性设置为零,如下所示:
Set your object's restitution
property to zero like this:
self.physicsBody.restitution = 0; //it's either self or the name of your object
如果这不能解决您的问题,请查看与您的对象相关的移动代码.寻找任何会导致来回"运动的情况,从而产生双重接触问题.
If that does not solve your issue, look at the movement code related to your object(s). Look for any situation that cause a 'back and forth' movement which can create the double contact issue.
作为最后的手段,您可以为您的联系人设置过滤器:
As a last resort you can set up a filter for your contacts:
- 创建一个存储联系时间的变量.
- 将联系人变量时间与 update: 方法中的当前时间进行比较.
- 如果差异小于您指定的时间(例如 0.2 秒),则允许联系并将您的联系时间变量设置为当前时间.如果差值低于过滤时间(0.2 秒),则忽略该联系人.
这篇关于spriteKit中联系人的双重通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!