cocos2d-iphonebitmap-fontscclabelttf

How do I animate the text on Lable like slot machine?


i would like to perform an animation on label text.i need to show 10 on label. the label should display first start with 0 then 1 ,2 ,3....end with 10.

example : showing display number animation is seems like in slot wheel.

how should i achieve that? is it any trick in cocos 2d?

Has anybody got a clue how to do that ?


Solution

  • First setup the label as an instance variable.

    // In your .h
    CCTabelTTF *_myLabeL;
    

    Then (assuming you have already added it in your project) create an update method for it

    - (void)updateLabel {
        int currentVal = [_myLabel intValue];
        NSString *newString;
    
        currentVal += 1;
        newString = [NSString stringWithFormat:@"%i", currentVal];
    
        [_myLabel setString:newString];
    }
    

    Now we can create a method to call your update:

    - (void)startUpdatingLabel:(int)newValue {
        int curVal = [_myLabel intValue];
        int difference = newValue-curVal;
        for(int i=0; i > difference; i++) {
            [self performSelector:@select(updateLabel) withObject:nil afterDelay:1.0];
        }
    }
    

    Then all you have to do whenever you want to update the label (say to 10) is to call

    [self startUpdatingLabel:10];
    

    and it will update your label by 1 every 1 second until it reaches the new value