-2

diamondText変数の場合: 、goldTextsilverText、およびという名前の 4 つのテキスト ビューbronzeText;money 変数unsigned int money;およびNSTimer.1 秒ごとに実行される関数:

-(void)updateMoney{
    money++;
    bronzeText.text = [NSString stringWithFormat:@"%d",money];
    silverText.text = [NSString stringWithFormat:@"%d",money%10];
    goldText.text   = [NSString stringWithFormat:@"%d",money%100];
    diamondText.text= [NSString stringWithFormat:@"%d",money%1000];
}

私の通貨はダイヤモンド = 10 ゴールド = 10 シルバー = 10 ブロンズ = 1 です。

お金のラベルを計算して表示する最も効率的な方法は何ですか? この変数を、GameCenter と NSDictionary、または GameCenter などと一緒にどのように保存しますか?


詳細は以下のとおりです。

明確にするために: ブロンズには最後の 2 つの数字があり、シルバーには次の 2 つの数字があります。

4つのintまたは配列を使用できることは理解していますが、より効率的な方法がない限り、この方法を使用したいと思います。

例: いつmoney = 1000; bronzeText = nothingsilverText = 10goldText = nothingdiamondText = nothing;

4

1 に答える 1

2

First off...if you're giving each coin two digits, then your math is off. If you mod 10 everything, then each coin only gets one digit. But you're not even doing that quite right; the math doesn't take into account the value of the coins, or ignore the coins you've already accounted for, or any of that. Say you had 1371 money...with your current math, bronze=1371, silver=1, gold=71, and diamond=371. I'm pretty sure that's not what you had in mind.

You might try something like

int bronze = money % 100;
int silver = (money / 100) % 100;
int gold = (money / 10000) % 100;
int diamond = money / 1000000;

Now, if you have 1371 money, you have bronze=71, silver=13, gold=0, diamond=0.

As for updating the views, with the bronze, you're pretty much going to always have to update that -- any change will affect it. If you're always incrementing by one, though, you only have to update the next higher coin when the current coin's count is 0. For example, if you had 2799 and updated, bumping your money up to 2800, you'd now have bronze=0, so you'd update silver to 28. Since 28 != 0, though, the gold wouldn't need updating, so you wouldn't even bother with diamond.

You only have to update it when the money changes, though. So doing it on collision makes a lot more sense than on a timer. :)

于 2012-09-15T16:44:02.983 に答える