简体   繁体   English

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

[英]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. 我使用以下代码绘制从触摸点A到触摸点B的线。该代码还允许我同时绘制多条线。

My first attempt was to create a NSUInteger and increase it by 1 per pixel moved in touchesMoved. 我的第一个尝试是创建一个NSUInteger,并将其在touchesMoved中移动的每个像素增加1。 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. 每次调用touchMoved时,都会为您提供该触摸的当前位置的xy坐标。 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. 因此,从touchBegan存储初始点,然后使用数学方法确定touchMoved中初始点和当前点之间的距离。

But also keep in mind that points don't necessarily equal pixels. 但也要记住,点不一定等于像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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