如何制作延伸链 [英] How To Make An Extending Chain

查看:10
本文介绍了如何制作延伸链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Okay, I have had a few questions floating around stackoverflow now, and most of them are related to this one question. I've been trying to get it myself, and I have a couple of times but the result is never quite what I expected. I am trying to develop a chain that my sprite can throw.

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

So far what I have done is thrown the lead of the hook from the centre of the sprite outward. Once it travels 10 pixels away from the sprites position it generates another link in the chain. The chain is then rotated to match the lead chains rotation and attached to it using a joint pin. It actually works fairly well. The only problem is it only works when I set the physics world speed to 0.01. If I return it to normal physics it throws the lead link in the chain but basically skips over everything else. Before this I tried containing the lead link in a physics body and calling a didEndContact to attach the other links but that didn't work nearly as well.

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

Does anyone have any ideas on how I can accomplish this? I just need the chain to extend from the sprites position, to a maximum length, and then afterwards retract. I had no idea it was going to be this difficult. Thank you in advance for all of your help, if you would like me to post my code I would be glad to, but considering I don't think it will work I haven't added it yet. Once again thank you very much in advance I have been racking my brain over this for weeks and it seems like I'm getting nowhere, although I have learnt many invaluable concepts which I am deeply appreciative to the stackoverflow community for.

推荐答案

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

This is a simple example of how to draw a line that is updated continuously...

- (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];
}

此方法检测由点起点和终点定义的线何时与一个或多个物理体相交.

This method detects when a line, defined by points start and end, intersects with one or more physics bodies.

- (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]];
    }];
}

这篇关于如何制作延伸链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆