Problem with CGRectInterectsRect

Discussion in 'iPhone/iPad Programming' started by Narcs, Jan 29, 2012.

  1. Narcs, Jan 29, 2012
    Last edited by a moderator: Jan 29, 2012

    macrumors newbie

    Joined:
    Jan 19, 2012
    #1
    I have created a method that sets this UIImageView's position to 35 pixels away from another UIImageView called mainSprite.

    Code:
    -(IBAction)hit{
        [self hitDummies];
        if (direction == 1) {
            [self hitDummies];
            hitBox.center = CGPointMake(mainSprite.center.x+30, mainSprite.center.y);
        }
        if (direction == 2) {
            [self hitDummies];
            hitBox.center= CGPointMake(mainSprite.center.x,mainSprite.center.y+30);
        }
        if (direction == 3) {
            [self hitDummies];
            hitBox.center = CGPointMake(mainSprite.center.x-30, mainSprite.center.y);
        }
        if (direction == 4) {
            [self hitDummies];
            hitBox.center = CGPointMake(mainSprite.center.x, mainSprite.center.y-30);
        }
    It does move it where I want. But there is another void method that tests if this hitBox intersects with another UIImageView: dummies.

    Code:
    -(void)hitDummies{
        if (CGRectIntersectsRect(hitBox.frame, dummies.frame)) {
            nextConvoBut.enabled = YES;
            nextConvoBut.hidden = NO;
        }
    }
    As you can see I am calling this method in -(IBAction)hit. Keep in mind "hit" is linked with "touch down".
    If you need to see any more code just ask :)
     
  2. macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #2
    Ok, so what's the question?
     
  3. thread starter macrumors newbie

    Joined:
    Jan 19, 2012
    #3
    The problem is that this is not working, sorry. It doesn't seem to be detecting the collision.
     
  4. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    What is "direction"?

    What is happening? What should be happening?

    Have you used breakpoints to determine what is getting called?

    Also, why not use

    Code:
    switch (direction)
    {
     case 1:... break;
     case 2:... break;
     default:... break;
    }
    Might look a little neater / cleaner / compact than a list of ifs.
     
  5. thread starter macrumors newbie

    Joined:
    Jan 19, 2012
    #5
    "direction" is an integer. Whenever the character goes in a different direction the integer changes. I have it so that if the integer is equal a certain number, hitBox will move in a certain direction. What should be happening is that when "hit" is being held, it should check if "hitBox" intersects with "dummies". But it doesn't seem to realize the intersection.
     

Share This Page