Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have two UIImageViews. mainSprite, and enemy1. And I have a method that makes enemy1 start moving towards mainSprite. So here's that method.

-(void)enemy1Aggro{
if (CGRectIntersectsRect(mainSprite.frame, enemy1Aggro.frame)) {
    if (mainSprite.center.x > enemy1.center.x) {
        enemy1.center = CGPointMake(enemy1.center.x+2, enemy1.center.y);
    }
    if (mainSprite.center.x < enemy1.center.x) {
        enemy1.center = CGPointMake(enemy1.center.x-2, enemy1.center.y); 
    }
    if (mainSprite.center.y > enemy1.center.y) {
        enemy1.center = CGPointMake(enemy1.center.x, enemy1.center.y+2); 
    }
    if (mainSprite.center.y < enemy1.center.y) {
        enemy1.center = CGPointMake(enemy1.center.x, enemy1.center.y-2);
    }
}

} This works because I'm calling it on an IBAction that is a button that gets held down. But the problem is that when that button stops being pressed, this method stops running. So what I need is a way to have this method keep on running. Maybe a for loop? I'm not using anything like cocos2d, and I'm not using CoreGraphics. If you need to see any more of my code just ask.

Thanks :)

share|improve this question
    
it looks like he said he is not using cocos2d... –  saiy2k Feb 18 '12 at 18:52
add comment

1 Answer

You can use NSTimer to setup something like gameloop which calls ur method again and again

Your code should look somethings like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:1/30.0 target:self selector:@selector(enemy1Aggro) userInfo:nil repeats:YES];
}

The above code will invoke the enemy1Aggro 30 times per second.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.