这是如何绘制该被不断更新的线的简单的例子...
- (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]];
}];
}
您是否考虑绘制一条线,该线随着时间的推移从精灵延伸到目的地? – 0x141E 2014-10-06 19:11:53
我想通过链条实现绳索物理学,但如果它是唯一可行的方法,我将不得不接受它。我是否也可以将一个物理机构附加到延伸线上?我是否会根据其长度增加来重新创建线条和其物理体? – Ryoku 2014-10-13 08:24:41
我创建了与SKPhysicsJointPins连接的链,但对于与其他物理机构交互的方式从未满意。 – 0x141E 2014-10-13 08:28:30