how to do a running score animation in iphone sdk

2019-05-24 22:51发布

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.

2条回答
戒情不戒烟
2楼-- · 2019-05-24 23:29

Try redrawing the view after each iteration where your score is being displayed:

for (/* loop conditions here */) {
    score += 1;
    [scoreView setNeedsDisplay:YES];
}
查看更多
我想做一个坏孩纸
3楼-- · 2019-05-24 23:33

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 updateScore method 15 times a second

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.

addToScore = 10;

Your new method updateScore would have a bit of code like this:

if (addToScore)
{
    addToScore--;
    currentScore++;
    // Now display currentScore
}
查看更多
登录 后发表回答