0

我创建了一个代码,通过触摸创建一条无限大小的线,我想知道如何限制大小,使线的开头和结尾可以是一个很小的距离到最大设定距离?

我使用的代码是:

pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
lineNode = [SKShapeNode node];
lineNode.path = pathToDraw;
lineNode.zPosition = 1000;
lineNode.strokeColor = [SKColor blueColor];
lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:pathToDraw];
lineNode.physicsBody.categoryBitMask = ballCategory;
lineNode.physicsBody.contactTestBitMask = ballCategory;
[self addChild:lineNode];

在触摸开始方法和

CGPathAddLineToPoint(pathToDraw, NULL, location.x, location.y);
lineNode.path = pathToDraw;
lineNode.zPosition = 1000;
lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:pathToDraw];
lineNode.physicsBody.categoryBitMask = boundaryCategory;
lineNode.physicsBody.contactTestBitMask = ballCategory;
lineNode.name = @"boundary";
lineNode.physicsBody.restitution=1;

在 touchMoved 方法中。

谢谢

4

1 回答 1

0

首先添加这个功能...

// Computes the distance between two points
static inline CGFloat distance(CGPoint p1, CGPoint p2)
{
    CGFloat dx = p1.x - p2.x;
    CGFloat dy = p1.y - p2.y
    return sqrt(dx*dx+dy*dy);
}

然后将此添加到您的触摸移动功能中...

CGFloat lineLength = distance(location, positionInScene);
if (distance < kMaxDistance) {
    // Draw line here

}

另外,我建议您等到知道该行有效后再创建 SKShapeNode。或许您应该创建一个占位符节点,例如圆形,然后在验证该线有效后创建形状节点线。

于 2014-07-24T16:31:25.457 回答