2014-10-04 70 views
0

好吧,我现在有几个问题在计算器周围浮动,其中大部分都与这个问题有关。我一直试图自己做,我有几次,但结果是从来没有我期望的。我正在尝试开发一个我的精灵可以抛出的链。如何制作延长链

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

有没有人有任何想法,我怎么能做到这一点?我只需要链条从精灵位置延伸到最大长度,然后缩回。我不知道这会很困难。如果您希望我发布我的代码,我会很高兴,但考虑到我认为它不会起作用,所以我还没有添加它。再次非常感谢你,我几个星期来一直在关注这个问题,虽然我已经学到了很多非常有价值的概念,但我对Stackoverflow社区深感欣慰。

+0

您是否考虑绘制一条线,该线随着时间的推移从精灵延伸到目的地? – 0x141E 2014-10-06 19:11:53

+0

我想通过链条实现绳索物理学,但如果它是唯一可行的方法,我将不得不接受它。我是否也可以将一个物理机构附加到延伸线上?我是否会根据其长度增加来重新创建线条和其物理体? – Ryoku 2014-10-13 08:24:41

+0

我创建了与SKPhysicsJointPins连接的链,但对于与其他物理机构交互的方式从未满意。 – 0x141E 2014-10-13 08:28:30

回答

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]]; 
    }]; 
} 
+0

太棒了非常感谢你!尽管我需要检测与它的碰撞,但链条有效。我也将不得不为其他物体的“头部”指定碰撞。我应该通过附加一个物理主体来行(检测整个主体的碰撞),然后在其前面添加一个带有自己的phyiscs主体(代表主体)的附加节点。或者你认为这会给不可预知的物理结果和/或丢帧率? – Ryoku 2014-10-13 08:42:30

+0

这取决于你想要模拟的东西。你可以在线头上的一个单独的精灵,并检测与之相冲突。为什么你需要检测线路是否与其他物理体碰撞? – 0x141E 2014-10-13 08:48:04

+0

我希望线条在与某些对象接触时切断 – Ryoku 2014-10-13 09:35:09