如何限制应用程序中触摸所做行的大小

时间:2014-07-19 19:32:23

标签: ios ios7 sprite-kit cgpath skshapenode

我已经创建了一个代码,可以通过触摸创建无限大小的线条,我想知道如何限制大小,使得线条的起点和终点距离最大设置距离可以很小?< / p>

我使用的代码是:

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方法中。

谢谢

1 个答案:

答案 0 :(得分: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之前等到知道该行有效。也许您应该创建一个占位符节点,例如圆圈,然后在验证该行有效后创建形状节点行。