ios - Sprite Kit bodyAtPoint 和 bodyInRect 返回不正确的值?

标签 ios ios7 sprite-kit skphysicsbody sknode

我创建了一个非常简单的示例代码,只有一个场景,一个 Sprite 节点 20x20px,位于屏幕上的 0.0 点。当我调用 scene.physicsWorld bodyAtPoint 时,它会返回我这个节点,即使在点例如:34x34。但在 35x35 处它返回 null。所以基本上两个轴上从 0px 到 34px 的所有点都返回这个节点,从 35px 开始,它不再返回它了。如果 Sprite 明显以 20px 20px 结束,知道可能是什么原因吗?在 bodyInRect 中可以看到相同的行为。

示例代码如下:

-(id)initWithSize:(CGSize)size {    
if (self = [super initWithSize:size]) {
    /* Setup your scene here */

    self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
    self.physicsWorld.gravity = CGVectorMake(0, 0);

    SKSpriteNode *node = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(20, 20)];
    node.position = CGPointMake(10, 10);

    node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 20)];
    [self addChild:node];

}
return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];
    if([self.physicsWorld bodyAtPoint:location])
    {
        NSLog(@"Detected at point: %f %f", location.x, location.y);
    }else{
        NSLog(@"No node at point: %f %f", location.x, location.y);
    }
}
}

这是控制台日志:

2013-11-09 15:05:52.822 SKTest[43143:70b] Detected at point: 2.000000 1.000000
2013-11-09 15:05:56.274 SKTest[43143:70b] Detected at point: 3.000000 7.000000
2013-11-09 15:05:57.006 SKTest[43143:70b] Detected at point: 3.000000 11.000000
2013-11-09 15:05:58.199 SKTest[43143:70b] Detected at point: 3.000000 12.000000
2013-11-09 15:05:58.918 SKTest[43143:70b] Detected at point: 3.000000 14.000000
2013-11-09 15:05:59.785 SKTest[43143:70b] Detected at point: 3.000000 17.000000
2013-11-09 15:06:00.685 SKTest[43143:70b] Detected at point: 3.000000 20.000000
2013-11-09 15:06:01.565 SKTest[43143:70b] Detected at point: 3.000000 22.000000
2013-11-09 15:06:02.915 SKTest[43143:70b] Detected at point: 3.000000 25.000000
2013-11-09 15:06:04.285 SKTest[43143:70b] Detected at point: 3.000000 30.000000
2013-11-09 15:06:05.387 SKTest[43143:70b] Detected at point: 3.000000 34.000000
2013-11-09 15:06:08.492 SKTest[43143:70b] No node at point: 4.000000 38.000000
2013-11-09 15:06:12.499 SKTest[43143:70b] Detected at point: 4.000000 5.000000
2013-11-09 15:06:13.240 SKTest[43143:70b] Detected at point: 6.000000 5.000000
2013-11-09 15:06:13.881 SKTest[43143:70b] Detected at point: 10.000000 5.000000
2013-11-09 15:06:15.064 SKTest[43143:70b] Detected at point: 12.000000 5.000000
2013-11-09 15:06:16.120 SKTest[43143:70b] Detected at point: 14.000000 5.000000
2013-11-09 15:06:16.873 SKTest[43143:70b] Detected at point: 16.000000 5.000000
2013-11-09 15:06:17.582 SKTest[43143:70b] Detected at point: 18.000000 5.000000
2013-11-09 15:06:18.066 SKTest[43143:70b] Detected at point: 21.000000 5.000000
2013-11-09 15:06:18.966 SKTest[43143:70b] Detected at point: 24.000000 5.000000
2013-11-09 15:06:19.585 SKTest[43143:70b] Detected at point: 31.000000 5.000000
2013-11-09 15:06:20.531 SKTest[43143:70b] Detected at point: 35.000000 5.000000
2013-11-09 15:06:22.581 SKTest[43143:70b] No node at point: 36.000000 4.000000
2013-11-09 15:06:26.560 SKTest[43143:70b] Detected at point: 19.000000 16.000000
2013-11-09 15:06:27.933 SKTest[43143:70b] Detected at point: 20.000000 17.000000
2013-11-09 15:06:29.856 SKTest[43143:70b] Detected at point: 25.000000 19.000000
2013-11-09 15:06:31.487 SKTest[43143:70b] Detected at point: 26.000000 22.000000
2013-11-09 15:06:33.850 SKTest[43143:70b] Detected at point: 29.000000 27.000000
2013-11-09 15:06:35.492 SKTest[43143:70b] Detected at point: 31.000000 29.000000
2013-11-09 15:06:36.854 SKTest[43143:70b] Detected at point: 35.000000 32.000000
2013-11-09 15:06:40.004 SKTest[43143:70b] No node at point: 40.000000 34.000000

nodeAtPointnodeInRect 是否有可能是 Apple 错误?难以相信。但是代码太短了,我看不出哪里有错误。 任何帮助将不胜感激。

最佳答案

我对此做了一些实验。当我放大 sprite 时,我注意到 body 区域似乎比 sprite 大。我必须将 sprite 大小除以 1.15 才能使主体矩形与显示的所有 4 个面的 sprite 大致匹配。不过不知道为什么。我从来没有遇到过游戏玩法问题,所以 nodeAtPoint 是否有阈值范围?

SKSpriteNode *node = [SKSpriteNode spriteNodetWithColor:[SKColor whiteColor]
                                                   size:CGSizeMake(200, 200)];
node.position = CGPointMake(200, 200);

CGRect bodySize = CGSizeMake(node.size.width / 1.15, node.size.height / 1.15);
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:bodySize];
[self addChild:node];

我在 Retina 4"和非 Retina iPad 模拟器上对此进行了测试。

关于ios - Sprite Kit bodyAtPoint 和 bodyInRect 返回不正确的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19876932/

相关文章:

ios - iOS 7:通过wi-fi或对等wi-fi使用Multipeer Connectivity连接设备

ios - 如何从 URL 中删除查询(用于 GET 参数)?

ios - 是否有等效于 gethostbyname 和 inet_ntoa 函数的任何 iOS SDK?

ios - CGRect 和触摸

ios - 从 CocoaPods 构建 Sinch

ios - skscene UIViewController 从 SKScene 发送短信

swift - 如何根据名称编辑 Sprite ?

ios - Sprite 套件在碰撞时获得零值

ios - 育儿时如何覆盖CALayers的绘图顺序

ios - 致命索引错误 ios