我正在开发一款游戏,我想创建一个暂停菜单.这是我的代码:
self.view?.paused = true
Run Code Online (Sandbox Code Playgroud)
但是NSTimer.scheduledTimerWithTimeInterval仍在运行..
for var i=0; i < rocketCount; i++ {
var a: NSTimeInterval = 1
ii += a
delaysShow = 2.0 + ((stimulus + interStimulus) * ii)
var time3 = NSTimer.scheduledTimerWithTimeInterval(delaysShow!, target: self, selector: Selector("showRocket:"), userInfo: rocketid[i], repeats: false)
}
Run Code Online (Sandbox Code Playgroud)
我希望time3在玩家点击暂停菜单时暂停计时器并在玩家回到游戏时继续运行计时器,但我怎么能暂停NSTimer.scheduledTimerWithTimeInterval?请帮帮我.
我想在应用程序处于活动状态并运行时在后台运行任务.任务应每2分钟左右重复执行一次.如果应用程序处于非活动状态,则应暂停"后台重复任务",如果应用程序再次处于活动状态,则应该继续应用.做这个的最好方式是什么 ?请帮忙.
这是我的iPhone秒表代码.它按预期工作,并在单击按钮时停止并恢复.
然而,当我点击"停止"时,计时器将不会在后台停止运行,当我点击"开始"恢复它时,它将更新时间并跳到当前的位置,而不是从停止的时间恢复.
我怎么能阻止NSTimer?是什么导致这种情况发生?
@implementation FirstViewController;
@synthesize stopWatchLabel;
NSDate *startDate;
NSTimer *stopWatchTimer;
int touchCount;
-(void)showActivity {
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss.SS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
[dateFormatter release];
}
- (IBAction)onStartPressed:(id)sender {
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1/10 target:self selector:@selector(showActivity) userInfo:nil repeats:YES];
touchCount += 1;
if (touchCount > 1)
{
[stopWatchTimer fire];
}
else
{
startDate = [[NSDate date]retain]; …Run Code Online (Sandbox Code Playgroud) 我知道这个问题已经被问到了,我已经完成了所有的帖子,但我仍然不能让它工作,我想我只是一个菜鸟.
这是问题所在.我按照了如何创建一个点击游戏的教程,我正在尝试添加一个暂停按钮,以便当玩家点击它时,计时器(仅30秒)暂停,而不是停止,然后恢复.我尝试过实现其他解决方案,但我无法集成它,程序不断崩溃.以下是我创建的方法:
//Pause Game Methods
-(void)pauseTimer {
if(isPaused == YES) {
//IMPLEMENT SOMETHING
}
}
- (IBAction)pauseButton:(UIButton *)sender {
isPaused = YES;
}
- (IBAction)resumeButton:(UIButton *)sender {
isPaused = NO;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.