Spritekit - 计算手指在两个事件之间移动的距离,以增加NSUInteger

时间:2016-02-26 23:26:15

标签: ios sprite-kit

我使用以下代码从点触摸点A到触点B绘制一条线。 该代码还允许我同时绘制多行。

我的第一次尝试是创建一个NSUInteger并在touchesMoved中将每个像素增加1。但是增量不一致。看起来快速移动手指会慢慢增加手数,而慢慢移动手指会快速增加手数。

我想我需要计算两个事件之间移动的距离,然后增加数字。我不知道怎么做,有人可以帮忙吗?

//在第一个触摸点

创建一条线
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
SKLabelNode *touchedNode = (SKLabelNode *)[self nodeAtPoint:positionInScene];

for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];
    // Create a mutable path
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:location];
    // Create a shape node using the path
    lineNode = [SKShapeNode shapeNodeWithPath:path.CGPath];
    lineNode.strokeColor = [SKColor blackColor];
    lineNode.glowWidth = 3.0;
    [_gameLineNode addChild:lineNode];
    // Use touch pointer as the dictionary key. Since the dictionary key must conform to
    // NSCopying, box the touch pointer in an NSValue
    NSValue *key = [NSValue valueWithPointer:(void *)touch];
    [lines setObject:lineNode forKey:key];
    }
}

//动态地将线条绘制到最后一个触摸点

- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
for (UITouch *touch in touches) {
    CGPoint location = [touch locationInNode:self];
    // Retrieve the shape node that corresponds to touch
    NSValue *key = [NSValue valueWithPointer:(void *)touch];
    lineNode = [lines objectForKey:key];
    if (lineNode != NULL) {
        // Create and initial a mutable path with the lineNode's path
        UIBezierPath *path = [UIBezierPath bezierPathWithCGPath:lineNode.path];
        // Add a line to the current touch point
        [path addLineToPoint:location];
        // Update lineNode
        lineNode.path = path.CGPath;
        lineNode.physicsBody = [SKPhysicsBody bodyWithEdgeChainFromPath:path.CGPath];
        lineNode.physicsBody.categoryBitMask = lineNodeCategory;
        lineNode.physicsBody.contactTestBitMask = bubble1Category | bubble2Category| bubble3Category | bubble4Category | bubble5Category | bubble6Category;
        lineNode.physicsBody.collisionBitMask = ballCategory | ballHalf1Category | ballHalf2Category;
        lineNode.physicsBody.dynamic = YES;
        lineNode.name = lineNodeCategoryName;

//ATTEMPT 1 - NSUINTEGER that increases per pixal moved
testNumber ++;
testLabel.text = [NSString stringWithFormat:@"%lu",(unsigned long)testNumber];

        }
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
inkLockOrUnlock = [[NSUserDefaults standardUserDefaults] boolForKey:@"inkLockOrUnlock"];

if (inkLockOrUnlock == NO) {
    SKAction *block0 = [SKAction runBlock:^{
        for (UITouch *touch in touches) {
            NSValue *key = [NSValue valueWithPointer:(void *)touch];
            [lines removeObjectForKey:key];
        }

        [_gameLineNode removeAllChildren];
    }];

    SKAction *inkAnimation = [SKAction sequence:@[block0,]];
    [self runAction:[SKAction repeatAction:inkAnimation count:1]];
} else {}
}

1 个答案:

答案 0 :(得分:0)

如果我理解你,我想你在这里可能会有很多误解。每次调用touchMoved时,它都会为您提供该触摸当前位置的xy坐标。该功能每帧最多只能调用一次。

听起来您认为触摸移动的每个像素都会调用该函数,但事实并非如此。

因此,存储touchBegan的初始点,然后使用math来确定初始点与touchMoved内当前点之间的距离。

但请记住,积分不一定等于像素。