0
\$\begingroup\$

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 :)

\$\endgroup\$
1
  • \$\begingroup\$ it looks like he said he is not using cocos2d... \$\endgroup\$ Commented Feb 18, 2012 at 18:52

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.