Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have three UIImageViews. enemy1, enemy1AggroBox and mainSprite. What I want to do is when mainSprite and enemy1AggroBox interect, I want enemy1 to start moving towards mainSprite. Basically creating aggro for a game.

if(CGRectIntersectsRect(mainSprite.frame, enemy1AggroBox.frame)){
//Code here//

My plan would be to call this method in viewDidLoad. I'm not using any sort of framework like cocos2d or OpenGLES. If you need to see any more code just ask.

share|improve this question
Are the rectangles always axis-aligned, or can they rotate? – Nic Foster Feb 17 '12 at 15:04
They don't rotate – Will Feb 17 '12 at 15:41
Are you checking for intersections frequently in a game loop or are you checking after some specific event? – willc2 Feb 17 '12 at 21:27
I'm checking frequently – Will Feb 18 '12 at 2:20
If you check frequently, you have to calculate the small position change for each time step closer to your target position then move the center point of enemy1 to that new position every frame. – willc2 Feb 18 '12 at 6:52

I'm not familiar with the particular platform you're working on, but I would imagine that a simple rectangle intersection function would solve this.

if ( intersects(mainSprite.frame, enemy1AggroBox.frame ) {
    MoveTowardPlayer(Player1, Enemy1);

bool intersects(const Rect& first, const Rect& second) const {
    if (first.Max.X < second.Min.X )
        return false; 

    if (first.Max.Y < second.Min.Y )
        return false;

    if (second.Max.X < first.Min.X )
        return false;

    return (second.Max.Y >= first.Min.Y )

Something like this would work provided your rectangles are Axis-Aligned (which you've indicated they are in your comment (below your question).

And to make one move towards the other (using pseudo-code, I don't know what engine you're using so I can't write exactly what you'll need to compile):

void MoveTowardPlayer(const Unit& player, const Unit& enemy) {
    // Get the difference in positions between player and enemy
    Vector2 direction = player.position - enemy.position;

    // Normalize the difference to get a direction vector
    direction.Normalize(); // Also sometimes called Unitize

    // Move the enemy in the direction of the player, based of the amount of
    // time has elapsed, and the enemy's movement speed, in units per second.
    enemy.position += (direction * enemy.movementSpeed * timeDelta);

I'm just giving a generic example on how to move your enemy toward a player. That question alone is pretty broad.

share|improve this answer
CGRectIntersectsRect() is a built-in function that does what your code does. – willc2 Feb 17 '12 at 21:20
anyway where is the code that makes the UIIMageview start going towards the other? – Will Feb 17 '12 at 21:31
Added that just now, the question on how to move one object toward another should be posted as a separate question, because it's fairly broad. I'm sure the answer to that exists on GameDev.StackExchange already anyhow. – Nic Foster Feb 17 '12 at 21:45

To make a UIView (or any subclass like UIImageView) move to a given point, in a given amount of time, do this.

NSTimeInterval totalDuration = 3.0; // three seconds  

// if you change any view property inside this block
// it will be smoothly interpolated over a duration     
[UIView beginAnimations:nil context:nil];  
[UIView setAnimationDuration:totalDuration]; =;  

[UIView commitAnimations];  
share|improve this answer
If you read the Programming Guide for Core Animation, you'll find Apple has done virtually all the heavy lifting already. – willc2 Feb 17 '12 at 23:08
This didn't seem to work. When they intersect the enemy1 image just immediately goes to the If it's easier maybe link me the part of the Apple Programming guide that shows how to accomplish what I'm trying? – Will Feb 18 '12 at 1:43
That method is for infrequent use and lets CA calculate how much a view needs to move each frame, automatically. -viewDidLoad is normally only called once, right after the UIView associated with some view controller has been fully set up. – willc2 Feb 18 '12 at 6:34
Either google for "Core Animation Programming Guide" or try this link… Try their sample code and don't imagine you can skip doing a fair amount of experimentation before you get a handle on it. There are also lots of YouTube videos and blog posts. – willc2 Feb 18 '12 at 6:45

Your Answer


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.