xcode 如何在iphone sdk中做跑分动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4851584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to do a running score animation in iphone sdk
提问by Jin
I wish to do a running score animation for my iphone app in xcode such that whenever I increase the score by an integer scoreAdded, the score will run up to the new score instead of being updated to the new score. I try some for loop with sleep but to no available. So I'm wondering if there's any way of doing it. Thank you.
我希望在 xcode 中为我的 iphone 应用程序做一个跑分动画,这样每当我将分数增加一个整数 scoreAdded 时,分数将运行到新的分数而不是更新到新的分数。我尝试了一些带睡眠的 for 循环,但没有可用。所以我想知道是否有任何方法可以做到这一点。谢谢你。
回答by Dave
Add a timer that will call a specific method every so often, like this:
添加一个定时器,它会每隔一段时间调用一个特定的方法,如下所示:
NSTimer *tUpdate;
NSTimeInterval tiCallRate = 1.0 / 15.0;
tUpdate = [NSTimer scheduledTimerWithTimeInterval:tiCallRate
target:self
selector:@selector(updateScore:)
userInfo:nil
repeats:YES];
This will call your updateScoremethod 15 times a second
这将每秒调用您的updateScore方法 15 次
Then in the main part of your game, instead of simply adding the amount to currentScore, I would instead store the additional amount in a separate member variable, say addToScore. e.g.
然后在游戏的主要部分,我不是简单地将数量添加到currentScore,而是将额外的数量存储在一个单独的成员变量中,比如addToScore。例如
addToScore = 10;
Your new method updateScorewould have a bit of code like this:
你的新方法updateScore会有一些像这样的代码:
if (addToScore)
{
addToScore--;
currentScore++;
// Now display currentScore
}
回答by Eimantas
Try redrawing the view after each iteration where your score is being displayed:
每次迭代后尝试重绘显示分数的视图:
for (/* loop conditions here */) {
score += 1;
[scoreView setNeedsDisplay:YES];
}