ゼロから 2 秒以内に達成されたスコアまでインクリメント (ランスルー) するカウンターを作成するにはどうすればよいですか? ゲーム オーバー ポップアップで最終スコアを表示するためにこれを使用する予定です。どうすればいいのかよくわかりません。助けてください。
質問する
185 次
2 に答える
0
以下は、指定された値に従って (スケジューラを使用して) アニメーションをセットアップするために使用できるコードです。
float secs = 2.0f;
float deciSecond = 1 / 10;
newScore = 100;
currentScore = 0;
scoreInDeciSecond = (newScore / secs) * deciSecond;
[self schedule:@selector(counterAnimation) interval:deciSecond];
そして、これはあなたのメソッドがアニメーションを処理する方法です:
- (void)counterAnimation {
currentScore += scoreInDeciSecond;
if (currentScore >= newScore) {
currentScore = newScore;
[self unschedule:@selector(counterAnimation)];
}
scoreLabel.string = [NSString stringWithFormat:@"%d", currentScore];
}
于 2013-10-18T13:52:15.987 に答える
0
個人的にはcocos2dがどのようにテキストを表示したり、タイマーを使ったりするのかはわかりませんが、純粋なiOS SDKでそれを行う方法は次のとおりです。cocos2d を知っていれば、変換するのに問題はないはずです。
- (void)viewDidLoad
{
[super viewDidLoad];
highScoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 75.0)];
[self displayHighScore];
}
-(void)displayHighScore {
highScore = 140;
currentValue = 0;
NSString* currentString = [NSString stringWithFormat:@"%d", currentValue];
[highScoreLabel setText:currentString];
[self.view addSubview:highScoreLabel];
int desiredSeconds = 2; //you said you want to accomplish this in 2 seconds
[NSTimer scheduledTimerWithTimeInterval: (desiredSeconds/highScore) // this allow the updating within the 2 second range
target: self
selector: @selector(updateScore:)
userInfo: nil
repeats: YES];
}
-(void)updateScore:(NSTimer*)timer {
currentValue++;
NSString* currentString = [NSString stringWithFormat:@"%d", currentValue];
[highScoreLabel setText:currentString];
if (currentValue == highScore) {
[timer invalidate]; //stop the timer because it hit the same value as high score
}
}
于 2013-10-18T14:00:47.623 に答える