简体   繁体   中英

Spritekit - calculate distance the finger has travelled between two events to increment your a NSUInteger

I use the following code to draw a line from point Touch point A to Touch point B. The code also allows me to draw multiple lines simultaneously.

My first attempt was to create a NSUInteger and increase it by 1 per pixel moved in touchesMoved. However the increment is not consistent. It appears that moving your finger fast will increase the number slowly whereas moving your finger slowly will increase the number fast.

I think I need to calculate the distance moved between two events and then increment the number. I don't know how to do this, can anyone help?

//Create a line at first point of touch

(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];
    }
}

// Draw the line to last touch point dynamically

- (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 {}
}

If I understand you, I think you might be misunderstanding a lot here. Every time touchMoved gets called, it gives you an xy coord for current position of that touch. That function can only be called max once per frame.

It sounds like you are thinking the function is called for every pixel the touch gets moved, which is not the case.

So store the initial point from touchBegan, then use math to determine distance between initial point and the current point inside touchMoved.

But also keep in mind that points don't necessarily equal pixels.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM