问题描述
我创建了一个非常简单的示例代码,只有一个场景,一个精灵节点20x20px,屏幕上的点0.0。当我调用 scene.physicsWorld bodyAtPoint
时,即使在例如:34x34点,它也会返回此节点。但是在35x35点,它返回 null
。所以基本上所有从0px到34px的点都返回此节点,从35px开始,它不再返回它。任何想法可能是什么原因,如果精灵明显以20px 20px结束?在 bodyInRect
中可以看到相同的行为。
I created a very simple sample code, just one scene, one sprite node 20x20px, at point 0.0 on screen. When I call scene.physicsWorld bodyAtPoint
it returns me this node even at point eg: 34x34. But at point 35x35 it returns null
. So basically all points from 0px to 34px in both axis returns this node, starting from 35px, it doesn't return it anymore. Any idea what could be the reason, if sprite visibly ends at 20px 20px? The same behaviour is seen in 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
这可能是一个Apple bug,在 nodeAtPoint
和 nodeInRect
?很难去相信。但是代码很短,以至于我无法看到这里可能出现的任何错误。
我们将非常感谢任何帮助。
Is that possible that it would be an Apple bug, in both nodeAtPoint
and nodeInRect
? It's hard to believe. But the code is so short that I can't see where any mistake could have been made here.Any help will be greatly appreciated.
推荐答案
我对此进行了一些实验。当我放大精灵时我注意到的是身体区域似乎比精灵更大。我不得不将精灵大小除以1.15以使主体矩形与所有4个侧面上显示的精灵大致匹配。不过不知道为什么。我从未遇到过游戏玩法的问题,所以nodeAtPoint可能有一个阈值范围?
I experimented a little with this. What I noticed when I enlarged the sprite is that the body area seemed to be larger than the sprite. I had to divide the sprite size by 1.15 to get the body rect to approximately match up with the displayed sprite on all 4 sides. No idea why, though. I never had an issue with gameplay, so perhaps nodeAtPoint has a threshold range?
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模拟器上进行了测试。
I tested this on the Retina 4" and non-Retina iPad Simulator.
这篇关于Sprite Kit bodyAtPoint和bodyInRect返回不正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!