感谢Maddy的提示。在触摸有关NSTimer的信息时,我得到了重复的操作。
我确实有它的每个点击屏幕的时间做一个动作:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[rocket.physicsBody applyForce:CGVectorMake(0,200)];
}
但这只会每点击触发。我希望它在触摸屏幕时继续施加力量。
计时器添加到类:
@interface TPMyScene()
@property (nonatomic, retain, readwrite) NSTimer * touchTimer;
@end
移动我的行动的方法:
-(void)boost {
NSLog(@"Boosting");
[rocket.physicsBody applyForce:CGVectorMake(0,200)];
}
触发计时器在的touchesBegan:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// First we need to trigger a boost in case the screen was just touched.
[rocket boost];
//set a timer to keep boosting if the touch continues.
//Also check there isn't already a timer running for this.
if (!self.touchTimer.isValid) {
self.touchTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:rocket selector:@selector(boost) userInfo:nil repeats:YES];
}
}
取消计时器触摸时以结束或取消结束:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.touchTimer invalidate];
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self.touchTimer invalidate];
}
你在找什么?如果你碰到触碰,那么它是连续的,直到你收到touchesEnded。 – rmaddy
^看看我上面的编辑 – user1855952