寻找与SKNode不起作用的寻路

时间:2016-02-29 06:09:25

标签: ios objective-c sprite-kit path-finding skphysicsbody

我一直在开发一种跟踪算法,如果精灵套件没有让我这么做的话,它会很好用...

我正在尝试确定CGPoint是否位于我场景中给定SKPhysicsBody的{​​{1}}内。我遇到的问题是我似乎无法让SpriteKit准确地告诉我物理实体是否确实包含了这一点。

以下是可能包含该点的一个实体的示例,以及我如何测试以查看它是否存在:

SKNode

然后我尝试了两种不同的方法来查看节点是否包含以下内容:

方法1

        SKSpriteNode *innerMap1 = [SKSpriteNode spriteNodeWithImageNamed:@"test"];
        // FYI - "test" is exactly 363x452. Did that to make sure scaling was not an issue
        innerMap1.physicsBody = [SKPhysicsBody bodyWithTexture:[SKTexture textureWithImageNamed:@"test"] size:CGSizeMake(363, 452)];
        innerMap1.position = CGPointMake(200, 100);
        innerMap1.physicsBody.dynamic = NO;
        innerMap1.name = @"testNode";

方法2

    CGPoint controlPoint = CGPointMake(<xf>, <yf>);
    if ([innerMap1 containsPoint:controlPoint]) . . .

无论出于何种原因,这两种方法都没有用。方法1将告诉我该点是否在节点的框架内,这违背了从纹理创建唯一节点形状的目的....并且方法2出于某种奇怪的原因往往是极其微不足道和不准确的。我读到大约有15.5px的超量程,但我得到的结果并没有接近这个最小值。

我也可以手动创建 SKPhysicsBody *body = [self.physicsWorld bodyAtPoint:controlPoint]; if ([body.node.name isEqualTo:@"testNode"]) . . . 并使用以下内容:

CGMutablePathRef

但同样,我想要不需要单独路径边界构建的自定义形状。

SO ,我的问题是,我做错了什么?我非常肯定方法2 本来是最好的解决方案,或者至少是工作,但我似乎无法理解为什么它的行为如此奇怪。也许我错过了解决我的问题的两种方法之一。我希望有一种方法可以为-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint touchLocation = [touch locationInNode:self.scene]; if (CGPathContainsPoint(pathRef, nil, touchLocation, NO)) { // If we are here it means user did touche physic body. } } 内的点返回BOOL,但当然没有。这就是为什么使用物理体应该是关键 - 因为它代表了精确的图像尺寸和点。任何建议将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:1)

很多时候我发现了CGPathContainsPoint输出的奇怪问题。 所以,我决定写下这个解决方法:

P.S。 (此解决方案适用于 Swift 2.x

let path: UIBezierPath = UIBezierPath.init(CGPath: myPath)
let copyPath = CGPathCreateCopyByStrokingPath(myPath, nil, path.lineWidth, path.lineCapStyle, path.lineJoinStyle, path.miterLimit )
if CGPathContainsPoint(copyPath, nil, myPoint, false) { 
   // ok path contain this point
}