问题
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.
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.
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.
回答1:
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];
}
EDIT: 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]];
}];
}
来源:https://stackoverflow.com/questions/26195900/how-to-make-an-extending-chain