我正在使用SpriteKit
创建一个应用,在SKSpriteNode
中创建一个touchesBegan
对象,然后当您在屏幕上平移时,它会随之移动对象。
但是,当我触摸屏幕时,精灵会按预期创建,然后当我移动手指时;精灵保持在同一个地方,小延迟然后移动到我的手指所在的位置。
我的代码如下:
-(void)handlePan: (UIPanGestureRecognizer*) gestureRecognizer{
if (([gestureRecognizer state] == UIGestureRecognizerStateBegan) || ([gestureRecognizer state] == UIGestureRecognizerStateChanged))
{
CGPoint newLocation = [scene convertPointFromView:[gestureRecognizer locationInView:self.view]];
scene.finger.position = newLocation;
}
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
NSLog(@"Ended");
[scene.finger removeFromParent];
scene.finger = nil;
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [self convertPointFromView:[touch locationInView:self.view]];
[self addFingerSprite:location];
}
}
任何想法,我如何解决这个小延迟问题?
答案 0 :(得分:1)
延迟是由使用PanGestureRecognizer
引起的。在系统识别出锅正在发生之前需要一小段时间。请改用touchesMoved
。