嗨我正在使用Xcode作为应用程序,我的评分系统每秒上升1点。如何让它从25开始,然后以每秒1点的速度上升。这是代码:
-(void)Scoring{
ScoreNumber = ScoreNumber + 1;
Score.text = [NSString stringWithFormat:@"Score: %i", ScoreNumber];
-(void)NewGame{
ScoreNumber = 0;
Score.text = [NSString stringWithFormat:@"Score: 0"];
请帮助!!
答案 0 :(得分:0)
在NewGame
中,您将ScoreNumber
设置为0.将其设置为25而不是:
-(void)NewGame{
ScoreNumber = 25;
Score.text = [NSString stringWithFormat:@"Score: 25"];
答案 1 :(得分:0)
试试这个:
- (void) newGameWithStartingScore:(int)startingScore {
// Set our label
Score.text = [NSString stringWithFormat:@"Score: %i", startingScore];
// Store our score in our dictionary
NSDictionary * userInfo = [@{@"currentScore" : @(startingScore)} mutableCopy];
// Start the timer
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(incrementScore:) userInfo:userInfo repeats:YES];
}
-(void)incrementScore:(NSTimer *)timer {
// Get current score
int currentScore = [timer.userInfo[@"currentScore"] intValue];
// Increment it
currentScore++;
// Update userInfo ref
timer.userInfo[@"currentScore"] = @(currentScore);
// Set our label
Score.text = [NSString stringWithFormat:@"Score: %i", currentScore];
// See if timer should continue
BOOL shouldInvalidate = NO;
/*
Insert validation logic here. Set 'shouldInvalidate' to YES in order to stop the timer
*/
if (shouldInvalidate) {
[timer invalidate];
}
}
假设Score
是对标签的引用,或者可以显示文本的内容,您可以这样称呼它:
[self newGameWithStartingScore:25];
这样,您可以根据需要进行修改。