感謝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