我正在使用Xcode for iOS制作游戏。这是我所拥有的一段代码,可以在点击屏幕时跳过精灵:
//tap/touch to jump (& play sound)
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event{
[self playSound];
jumpUp = 16;
}
我怎样才能实现它,以便精灵不断上升而你只是轻触屏幕而不是一次点击?
//Pseudo code:
while touchingScreen {
jumpUp +=1;
}
答案 0 :(得分:0)
你需要在触摸持续时运行的touchesBegan
中启动某种循环。然后在touchesEnded
(并取消!)使该循环停止。您可以使用重复的NSTimer或类似以下内容。您可能需要进行一些调整,以使游戏更加流畅。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
// touching is a BOOL that keeps track of the touch event
// YES means that a touch is happening at the moment
touching = YES;
dispatch_async(dispatch_get_main_queue(), ^{
[self performSelector:@selector(up) withObject:nil afterDelay:.0];
});
}
- (void)up {
// move your sprite further up
NSLog(@"up");
if (touching) {
// if the user is still touching repeat moving the sprite up
[self performSelector:@selector(up) withObject:nil afterDelay:0.1];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
// The finger was lifted, stop the up movement
touching = NO;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
touching = NO;
}