当用户触摸屏幕时,它会使用if查看布尔值,并在值为NO
时触发计时器。它将其设置为YES
,因此用户无法再次触发它。当计时器完成它正在做的事情时,它会将其设置回NO
。该值在viewDidLoad方法中初始化。它工作一次,但不会再工作。从事物看来,它应该工作。任何人都可以看到这有什么问题:
- (void) fireLoop {
fireImage.hidden = 0;
fireImage.center = CGPointMake(fireImage.center.x, fireImage.center.y - 20);
if (fireImage.center.y == -5) {
[fireTimer invalidate];
fireImage.hidden = 1;
firedOn = NO;
}
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (firedOn == NO) {
firedOn = YES;
//set fire image position to player position
fireImage.center = CGPointMake(playerImage.center.x, playerImage.center.y);
fireTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/30 target:self selector:@selector(fireLoop) userInfo:nil repeats:YES];
}
}
答案 0 :(得分:4)
仅当fireImage.center.y正好是-5
时才重置firedOn变量 - 检查它是否真的如此。如果不是 - 您可能需要调整该条件(例如,检查y是否小于-5,或者它是否位于某个区间而不是1个精确值)