0

So, I have been working on this code for way to long for one day. I am sure there has to be a simple way to solve this but I can't think anything right now. First a small section of the code:

        if(Settings.totalHits >= 50 && Settings.totalHits <= 99)
        {
           Settings.medal1 = true;
           Settings.save(game.getFileIO());
           totalMedals = totalMedals + 1;
        }

This is a very basic achievement/medal system. Basically, once the user has done a total of 50 hits, it sets the value of "medal1" to true, which will remove the "lock" image which only displays when medal1 = false.

The issue deals with the totalMedals section. Basically, there is a total of 32 medals a user can earn, and after one is unlocked, it needs to update the totalMedals by 1. So in this case, the output would be "1/32". Except, now after the medal is unlocked (by hitting 50), the lock image is removed correctly, but the totalMedals keeps increasing by 1 extremely fast instead of just increasing to "1" and stopping.

Like I said, there has to be something small that I am overlooking here. I tried tossing into a for loop but that didn't work (or I did it wrong). Any idea what I can change to fix this?

4

2 に答える 2

3

Instead of

if(Settings.totalHits >= 50 && Settings.totalHits <= 99)

Try:

if(Settings.totalHits >= 50 && Settings.totalHits <= 99 && !Settings.medal1)

This way, once the medal has been triggered, it cannot be triggered again. Thus, totalMedals is only incremented once.

于 2013-04-24T03:29:09.037 に答える
0

しきい値に達し、まだロックを解除していない場合にのみメダルのロックを解除する必要があるため (上限はここでは関係ありません)、次を使用できます。

if (Settings.totalHits >= 50 && !Settings.medal1) {
    Settings.medal1 = true;
    Settings.save(game.getFileIO());
    totalMedals++;
}

if (Settings.totalHits >= 100 && !Settings.medal2) {
    Settings.medal2 = true;
    Settings.save(game.getFileIO());
    totalMedals++;
}

// and so on.

もちろん、これは、配列を効率的に使用するために、次のようなリファクタリングを積極的に呼びかけています。

for (i = 1; i < 33; i++) {
    if (Settings.totalHits >= 50 * i && !Settings.medal[i-1]) {
        Settings.medal[i-1] = true;
        Settings.save(game.getFileIO());
        totalMedals++;
    }
}

また、しきい値が 50 の正倍数でない場合は、必要に応じて任意の複雑な関数 (または配列ルックアップ) をifステートメントに簡単に配置できます。

于 2013-04-24T03:31:59.910 に答える