-4

When the user gets coins or uses coins I'd like them to experience their coins being minus-ed live (-) for example instead of the label displaying {score = score -10} and all of a sudden 10 points disappear:

instead they should see the points being deducted in an animated way 50 - 49 - 48 - 47 - 46 and so on till 10 points are fully minus-ed.

code:

[scoreLabel setText:[NSString stringWithFormat:@"score: %d", score]]; 

code: for e.g: - 100 | - 10 | +3 | +5

for (int i = 1; i<= 10; i++) { NSLog (@“%d”, i);
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Jelter
  • 7
  • 3
  • 1
    The code you've shown is for showing the score in a label, which is what you don't want. What is the code that you've tried for what you actually want to do? – Abizern Mar 28 '14 at 19:12
  • @Abizern I've tried to display the code in images through an NSArray but its not the right way, then I tried to animate it some how but keep hitting a brick wall. but I'm here to learn most of all, and will accept tuts if you know any! – Jelter Mar 28 '14 at 19:16
  • Start [here](https://developer.apple.com/library/iOS/referencelibrary/GettingStarted/RoadMapiOS/index.html) and keep going. Good luck. – Abizern Mar 28 '14 at 19:18

3 Answers3

1

Hi Jelter,

You can try this. It will work...

//Create a timer object

@property (strong, nonatomic) NSTimer *timer;

//Score label

@property (weak, nonatomic) IBOutlet UILabel *score;

// Call this when you want to update score I Assume on press of a button you update score.

- (IBAction)scoreButtonPressed:(id)sender {
    self.timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self   selector:@selector(updateScore) userInfo:nil repeats:YES];
}

-(void)updateScore{
    static int i = 0;
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0;
    animation.type = kCATransitionFade;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.score.layer addAnimation:animation forKey:@"changeTextTransition"];

    // Change the text
    self.score.text = [NSString stringWithFormat:@"%d", [self.score.text intValue] - 1];
    i++;
    if (i == 10 && [self.timer isValid]) {
        [self.timer invalidate];
        i = 0;
    }
  }
Dipak
  • 2,263
  • 1
  • 21
  • 27
  • He he he, No it didn't - in facet if I use that my users would finish the game without even trying! – Jelter Mar 28 '14 at 20:44
  • I need to give the user +5 coins, and I need them to watch the coins load up if they get 5 coins and they have 55 - the coin label should show the coins adding up one by one! – Jelter Mar 28 '14 at 20:47
  • 2
    I don't understand what u exactly want. Added a project on https://github.com/DeepakIosDev/ShowLabelAnimation that update score label when tap on update score button. You make score logic according to need. This project simply update score label and you experience your score being minus-ed. – Dipak Mar 29 '14 at 04:34
-1

Typed this in without testing:

//currentScore keeps track of current score.  Defined in interface
//timer defined in interface
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self   selector:@selector(updateScore:) userInfo:@(currentScore - 10) repeats:YES];


- (void) updateScore:(NSNumber *)targetScore
{
    currentScore--;

    [scoreLabel setText:[NSString stringWithFormat:@"score: %d", currentScore]]; 

    if (currentScore==[targetScore intValue])
    {
        //Exit timer when goal is reached
        [timer invalidate];
    }
}
Abizern
  • 146,289
  • 39
  • 203
  • 257
jd.
  • 84
  • 6
-2

You can do this with a for loop. You'll need to add a delay since this will happen so fast that you cannot see it. This might help you Objective-C delay action with blocks

-(void)subtractIntegerFromScore:(int)integer {
    for (int i = 0; i < integer; i++) {
        score = score - 1;
        [scoreLabel setText:[NSString stringWithFormat:@"score: %d", score]]; ;
    }
}
Community
  • 1
  • 1
Hackmodford
  • 3,901
  • 4
  • 35
  • 78