为什么didBeginContact被多次调用? [英] Why are didBeginContact called multiple times?

查看:372
本文介绍了为什么didBeginContact被多次调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个使用Sprite Kit的iOS游戏中,在Sprite Kit的内置物理引擎中使用接触检测,每次与敌人接触时,我的英雄的生命数减少一次。这是从 didBeginContact 方法完成的。
然而,似乎该方法不只是调用一次,当联系开始,但只要英雄和敌人重叠,持续调用:当我在该方法中设置断点,我可以看到,它是与 contact.bodyA contact.bodyB 存在的完全相同的物理实体实例。结果是,英雄将失去多个生命,即使他只传递一个单一的敌人。

In an iOS game that uses Sprite Kit along with the contact detection in Sprite Kit's build-in physics engine, I decrease the Hero's number lives by one each time he gets in contact with an enemy. This is done from the didBeginContact method. However, it seems like that method is not just called once, when the contact begins, but called continuously as long as the Hero and the enemy overlaps: when I set a breakpoint in that method, I can see, that it is the exact same physics body instances that exist as contact.bodyA and contact.bodyB. The result is, that the Hero will lose multiple lives, even though he only passes one single enemy.

如果英雄再次遇到同一个敌人,他应该得到一个更多的活减去,因此我不能只保留 seenEnemies 哈希设置来处理上面的问题。

If the Hero meets the same enemy again later, he should get one more live subtracted, and therefore I cannot just maintain a seenEnemies hash set to deal with the problem above.

问题是:你如何确保每个英雄/敌人联系人只减去一个生命?

The question is now: how would you make sure that only one live is subtracted for each Hero/enemy contact?

推荐答案

我有同样的问题(单个敌人的分数增加多次,单个实例的多个生命点的损害。)Apple论坛上的用户认为它是 [SKPhysicsBody bodyWithTexture:size:] 中的错误,但我不相信这种情况,因为它也发生在其他构造函数。

I had the same problem (score increasing multiple times for a single enemy destroyed and multiple life points being lost for a single instance of damage.) A user on the Apple forums thinks that it's a bug in [SKPhysicsBody bodyWithTexture:size:] but I don't believe that's the case, because it was happening with other constructors too.

首先, categoryBitMask contactTestBitMask 很重要,很明显。查看Apple的 SpriteKit物理学碰撞示例代码

First off, the categoryBitMask and contactTestBitMask are very important, obviously. Take a look at Apple's SpriteKit Physics Collisions sample code:


//联系人通常是双调度问题;所需的效果基于联系人中两个实体的类型。这个样本这是一个强力的方式,通过检查每个的类型。一个更复杂的例子可能使用对象上的方法来执行类型检查。

// Contacts are often a double dispatch problem; the effect you want is based on the type of both bodies in the contact. This sample this in a brute force way, by checking the types of each. A more complicated example might use methods on objects to perform the type checking.

// 联系人可以以任意顺序你需要检查
每一个对另一个。在此示例中,类别类型排序良好,因此
如果两个实体无序,则交换两个实体。 这允许代码
只测试一次冲突。

在处理每个条件后设置标志。在我的情况下,我测试 bodyA.node.parent 是否在 didBeginContact 中为nil,因为我调用 removeFromParent()在导弹/敌方节点上消灭它们。

What I did to solve it was setting a flag after handling each condition. In my case, I was testing whether bodyA.node.parent was nil in didBeginContact, because I called removeFromParent() on the missile/enemy nodes to destroy them.

我想你应该期待事件多次发射您的代码必须确保它只处理一次。

I think you should expect the event to fire multiple times and your code in there has to make sure it's processed only once.

这篇关于为什么didBeginContact被多次调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆