Reputation: 45
How do i run a loop once per event? Consider the following code:
-(void)didloadFromCCB {
BOOL add = YES;
if (bought_coins && add)
{
coins = coins + 5;
add = NO;
}
}
The problem with this code is that coins are added to the player every time the game restarts after the player bought the coins ONCE. I want the coins to be added ONLY when the player buys the coins each time and not every time the game loads from CCB. How should i change the code to make it work?
Upvotes: 0
Views: 126
Reputation: 212979
You need to make add
a static variable, so that its value persists across calls to didLoadFromCCB
:
-(void)didloadFromCCB {
static BOOL add = TRUE;
// ^^^^^^
if (bought_coins && add)
{
coins = coins + 5;
add = FALSE;
}
}
If you need to subsequently reset add
(e.g. when coins are purchased again) then you could move it outside of the function, e.g.
static BOOL add = TRUE;
-(void)didloadFromCCB {
if (bought_coins && add)
{
coins = coins + 5;
add = FALSE;
}
}
Upvotes: 1