I am trying to create a for loop within a method that will animate a computer controlled enemy player a predetermined distance 6 times whenever it is the enemy’s turn. Currently with the below code the enemy moves toward the player’s character, but the loop runs it far too quickly, so instead of the enemy animating each time it moves, it only animates the final movement.
Essentially what I am attempting to do is at the end of the loop cause a short (.75 second) delay to slow the looping down to an acceptable amount. I have searched high and low on the internet for this information and I’m surprised I can’t find the answer. It seems like it would be incredibly simple. Any help would be greatly appreciated!
for (int i=0; i<6; i++) {
// Enemy NE
if (enemyZombie.center.x < orcIdle.center.x && enemyZombie.center.y > orcIdle.center.y){
[UIView animateWithDuration:.75 animations:^{ enemyZombie.center = CGPointMake(enemyZombie.center.x + 42.5, enemyZombie.center.y - 30); }];
}
// Enemy NW
if (enemyZombie.center.x > orcIdle.center.x && enemyZombie.center.y > orcIdle.center.y){
[UIView animateWithDuration:.75 animations:^{ enemyZombie.center = CGPointMake(enemyZombie.center.x - 42.5, enemyZombie.center.y - 30); }];
}
// Enemy SE
if (enemyZombie.center.x < orcIdle.center.x && enemyZombie.center.y < orcIdle.center.y){
[UIView animateWithDuration:.75 animations:^{ enemyZombie.center = CGPointMake(enemyZombie.center.x + 42.5, enemyZombie.center.y + 30); }];
}
// Enemy SW
if (enemyZombie.center.x > orcIdle.center.x && enemyZombie.center.y < orcIdle.center.y){
[UIView animateWithDuration:.75 animations:^{ enemyZombie.center = CGPointMake(enemyZombie.center.x - 42.5, enemyZombie.center.y + 30); }];
}
}
You can use the completion block in UIView’s selector
animateWithDuration:animations:completion:to recursively perform the next step in your loop like this:Haven’t tested the code, but you’ll get the idea 🙂