Set up a-complicated?-timer Obj-C

What I'm trying to do is make an automated battle scene.


The time will occur every second and on the first second the hero will do damage to the enemy which will lower the enemies health by the attack variable (see below) and if the enemies health is higher than 0 then on the 2nd second (or just right after the hero's attack ends if that's easier to code) the enemy will attack and lower the hero's hp by the amount of the enemies attack. This continues until either the hero or the enemy's health is <= 0. At that point the timer is stopped.


Here is my code:

int heroHp = 100;
int heroAtk = 50;

int enemy001Hp = 100;
int enemy001Atk = 1;

int enemy002Hp = 600;
int enemy002Atk = 50;

bool battleActive = NO;

int useless;

- (IBAction)fight:(id)sender {
    if (battleActive == NO) {
        battleActive = YES;
        self.heroHpLabel.text = [NSString stringWithFormat:@"%i",heroHp];
        self.heroAtkLabel.text = [NSString stringWithFormat:@"%i",heroAtk];
        self.enemyHpLabel.text = [NSString stringWithFormat:@"%i", enemy001Hp];
        self.enemyAtkLabel.text = [NSString stringWithFormat:@"%i", enemy001Atk];
       //set timer here
    } else if (battleActive == YES) {
        useless = 1;
    }
}



Thanks

If you read the documentation for NSTimer, you will find methods such as +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: which will set up a timer to call a method of your choice at an interval you specify. Each time your method is called you can perform your calculations and update the UI.

Set up a-complicated?-timer Obj-C
 
 
Q