Troy R
Troy R

Reputation: 426

How to keep variable value across multiple method invocations?

I would like to use a button once in my Cocos2D game.

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    int f = 0;
    if (![self buttonTouch:touch]) return NO;
    if (f !=  1) {
        f = 1;
        button1.color = ccc3(50, 50, 50);
    }
    return YES;
}

The button works perfectly but I only want it to work once. As you can see at the start f=0 and once action is activated it checks if its not been used before and then changes to f=1 (button used) so action can not be played again. But for some reason it does not work, any ideas?

Upvotes: 2

Views: 165

Answers (1)

Guru
Guru

Reputation: 22042

Use static Or make it member variable.

     static int f = 0;

Upvotes: 1

Related Questions