0

好的,我现在在 stackoverflow 周围有几个问题,其中大部分都与这个问题有关。我一直试图自己得到它,我有几次,但结果从来都不是我所期望的。我正在尝试开发一个我的精灵可以抛出的链。

到目前为止,我所做的是将钩子的引线从精灵的中心向外抛出。一旦它从精灵位置移动 10 个像素,它就会在链中生成另一个链接。然后旋转链条以匹配引导链的旋转并使用连接销连接到它。它实际上工作得很好。唯一的问题是它仅在我将物理世界速度设置为 0.01 时才有效。如果我将它恢复到正常的物理状态,它会抛出链中的引导链接,但基本上会跳过其他所有内容。在此之前,我尝试在物理主体中包含前导链接并调用 didEndContact 来附加其他链接,但这几乎没有效果。

有人对我如何做到这一点有任何想法吗?我只需要链条从精灵位置延伸到最大长度,然后缩回。我没想到会这么难。提前感谢您的所有帮助,如果您希望我发布我的代码,我会很高兴,但考虑到我认为它不会起作用,我还没有添加它。再次非常感谢你,我已经为此绞尽脑汁好几个星期了,似乎我无处可去,尽管我学到了许多宝贵的概念,我非常感谢 stackoverflow 社区。

4

1 回答 1

1

这是一个简单的例子,说明如何绘制一条不断更新的线......

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    startingPoint = positionInScene;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    // Remove temporary line if it exist
    [lineNode removeFromParent];

    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, startingPoint.x, startingPoint.y);
    CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);

    lineNode = [SKShapeNode node];
    lineNode.path = pathToDraw;
    CGPathRelease(pathToDraw);
    lineNode.strokeColor = [SKColor whiteColor];
    lineNode.lineWidth = 1;
    [self addChild:lineNode];
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];

    // Remove temporary line
    [lineNode removeFromParent];

    CGMutablePathRef pathToDraw = CGPathCreateMutable();
    CGPathMoveToPoint(pathToDraw, NULL, startingPoint.x, startingPoint.y);
    CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);

    SKShapeNode *finalLineNode = [SKShapeNode node];
    finalLineNode.path = pathToDraw;
    CGPathRelease(pathToDraw);
    finalLineNode.strokeColor = [SKColor redColor];
    finalLineNode.lineWidth = 1;
    [self addChild:finalLineNode];
}

编辑:此方法检测由点开始和结束定义的线何时与一个或多个物理实体相交。

- (void) rotateNodesAlongRayStart:(CGPoint)start end:(CGPoint)end
{
    [self.physicsWorld enumerateBodiesAlongRayStart:start end:end
                    usingBlock:^(SKPhysicsBody *body, CGPoint point,
                                    CGVector normal, BOOL *stop)
    {
        SKNode *node = body.node;
        [node runAction:[SKAction rotateByAngle:M_PI*2 duration:3]];
    }];
}
于 2014-10-13T08:30:58.760 回答