【问题标题】:SKShapeNode Detect Two Lines IntersectionSKShapeNode 检测两条线的交点
【发布时间】:2015-05-30 09:48:15
【问题描述】:

我正在开发一个应用程序,我正在根据用户手指的用户触摸画线。一旦收到触摸结束事件,该行将转换为最后一条路径。当接收到新的触摸开始事件时,将绘制一条名为“当前路径”节点的新线。我为具有相反接触位掩码的两条线添加了一个物理体,但我无法接收碰撞事件。 以下是我的代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    currentPath = CGPathCreateMutable();
    currentPathNode = [self newLineNodeWithFillColor :  CURRENT_LINE_COLOR];
    CGPathMoveToPoint(currentPath, NULL, positionInScene.x, positionInScene.y);
    currentPathNode.path = currentPath;
    [self addChild:currentPathNode];
    uint32_t contactBitMask = circleCategory | lastPathCategory;
    [self addPhysicsBodyForLine:currentPathNode withCategoryBitMask:drawPathCategory withContactBitMask:contactBitMask];
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    CGPathAddLineToPoint(currentPath, NULL, positionInScene.x, positionInScene.y);
    currentPathNode.path = currentPath;
    uint32_t contactBitMask = lastPathCategory;
    [self addPhysicsBodyForLine:currentPathNode withCategoryBitMask:drawPathCategory withContactBitMask:contactBitMask];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(lastPath == nil){
        lastPath = CGPathCreateMutable();
    }
    CGPathAddPath(lastPath, nil, currentPath);
    [lastPathNode removeFromParent];
    if(currentPathNode != nil){
        [currentPathNode removeFromParent];
        currentPathNode = nil;
    }
    lastPathNode = [self newLineNodeWithFillColor : LAST_LINE_COLOR];
    lastPathNode.path = lastPath;
    [self addChild:lastPathNode];
    [self addPhysicsBodyForLine:lastPathNode withCategoryBitMask:lastPathCategory withContactBitMask:drawPathCategory];
    CGPathRelease(currentPath);
}  
- (void) addPhysicsBodyForLine:(SKShapeNode*)node withCategoryBitMask:(uint32_t)category withContactBitMask:(uint32_t)contactBitMask{
    node.physicsBody =  [SKPhysicsBody bodyWithEdgeChainFromPath:node.path];
    node.physicsBody.categoryBitMask    = category;
    node.physicsBody.contactTestBitMask = contactBitMask;
    node.physicsBody.collisionBitMask = contactBitMask;
    node.physicsBody.dynamic          = YES;
    node.physicsBody.usesPreciseCollisionDetection = YES;
}

但是没有检测到碰撞?任何解决方案。

【问题讨论】:

    标签: ios iphone swift sprite-kit skspritenode


    【解决方案1】:

    碰撞不能这样工作。如果您使用物理移动节点的位置,您只会记录碰撞。在已经存在的物理体上方(或跨过)创建新的物理体不会记录碰撞。

    您可以在每次绘制新路径时使用-(BOOL)intersectsNode:(SKNode *)node 来检查新节点是否与任何其他节点相交。

    【讨论】:

      猜你喜欢
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多