Spritekit触及陷入滞后

时间:2014-03-23 06:37:54

标签: ios sprite-kit

我正在用spritekit制作游戏,触摸开始和触摸结束之间有一个明显的100-200ms滞后。

有什么方法可以加快速度吗?我需要使用触摸结束(计算用户触摸的起点和终点之间的矢量光线。

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        touch_start_pt = location;
    }

    touching = true;
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        double distance = sqrt(pow(location.x - touch_start_pt.x, 2) + pow(location.y - touch_start_pt.y, 2));

        if(distance > 2 && touching && !paused){
            [self impulsePlayer:location];
        }
    }

    touching = false;
}



-(void) impulsePlayer : (CGPoint) location{
    touching = false;

    player.physicsBody.velocity = CGVectorMake(0, 0);

    double dx = location.x - touch_start_pt.x;
    double dy = location.y - touch_start_pt.y;

    CGVector impulse_vector = CGVectorMake(dx*main_impulse_divisor, dy*main_impulse_divisor);

    [player.physicsBody applyImpulse:impulse_vector];
}

日志:

2014-03-23 02:50:26.000 Impakt[2398:60b] began
2014-03-23 02:50:26.532 Impakt[2398:60b] ended
2014-03-23 02:50:29.149 Impakt[2398:60b] began
2014-03-23 02:50:29.648 Impakt[2398:60b] ended
2014-03-23 02:50:34.368 Impakt[2398:60b] began
2014-03-23 02:50:34.815 Impakt[2398:60b] ended

1 个答案:

答案 0 :(得分:3)

大多数UIGestureRecognizer实例会延迟转发触摸事件,直到它们被识别为#34;他们的姿态并没有得到认可。这将导致toucheBegan和/或touchesEnded消息被延迟。

您可以通过手势识别器实例的delayTouchesBegan and delayTouchesEnded属性更改此行为。