簡體   English   中英

Spritekit-計算手指在兩個事件之間的移動距離,以增加NSUInteger

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

我使用以下代碼繪制從觸摸點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 {}
}

如果我了解您,我認為您在這里可能會誤會很多。 每次調用touchMoved時,都會為您提供該觸摸的當前位置的xy坐標。 每幀只能調用一次該函數。

聽起來您好像在想觸摸移動的每個像素都會調用該函數,情況並非如此。

因此,從touchBegan存儲初始點,然后使用數學方法確定touchMoved中初始點和當前點之間的距離。

但也要記住,點不一定等於像素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM