SpriteKit:didBeginContact在iPad上不停调用 [英] SpriteKit: didBeginContact being called non-stop on iPad

查看:247
本文介绍了SpriteKit:didBeginContact在iPad上不停调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的项目,使用SpriteKit,一个怪物被丢弃并穿过一个平台。在iPhone上,当怪物击中平台时,didBeginContact会被调用一次。然而,在iPad上,当怪物在平台上滑动时,该方法每秒被调用几次。为什么iPhone上的它很好但是iPad上有错误?我已经在模拟器和实际iPad上进行了测试。

I have a very simple project using SpriteKit where a monster is dropped and walks across a platform. On the iPhone, didBeginContact gets called once when the monster hits the platform. However, on the iPad, the method gets called several times a second as the monster slides across the platform. Why is it fine on the iPhone but buggy on the iPad? I've tested on both the simulator and an actual iPad.

这里是我设置类别的地方

Here is where I set up the Categories

static const uint32_t playerCategory        = 0x1 << 0;
static const uint32_t bulletCategory        = 0x1 << 1;
static const uint32_t monsterCategory       = 0x1 << 2;
static const uint32_t platformCategory      = 0x1 << 3;
static const uint32_t worldCategory         = 0x1 << 4;

以下是怪物的设置

enemy.physicsBody.dynamic = YES;
enemy.physicsBody.affectedByGravity = YES;
enemy.physicsBody.allowsRotation = NO;
enemy.physicsBody.categoryBitMask = monsterCategory;
enemy.physicsBody.contactTestBitMask = worldCategory & platformCategory & bulletCategory;
enemy.physicsBody.collisionBitMask = platformCategory;

以下是怪物被移动的方式

Here is how the monster gets moved

enemy.physicsBody.velocity = CGVectorMake(100, 0);

这就是我知道didBeginContact被不断调用的方式。我每秒钟得到一次日志,说怪物击中平台。我需要在以后实现新内容时修复此问题。

And here is how I know didBeginContact gets called constantly. I get a log 5 times every second saying monster hit platform. I need to fix this for when I implement new things later.


if (firstBody.categoryBitMask == monsterCategory && secondBody.categoryBitMask == platformCategory) {
    NSLog(@"Monster Hit Platform");
}



推荐答案

enemy.physicsBody.restitution = 0;
platform.physicsBody.restitution = 0;

这是bounciness属性。这样,当敌人和平台发生碰撞时,不会有很少的弹跳触发多个接触事件。

This is the "bounciness" property. This way, when the enemy and platform collide, there will not be little bounces triggering multiple contact events.

我还看到建议使用applyImpulse而不是手动设置速度。

I have also seen that it is recommended to use applyImpulse instead of setting the velocity manually.

这篇关于SpriteKit:didBeginContact在iPad上不停调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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