imageviews not detecting collision

Member
Posts: 75
Joined: 2009.01
Post: #1
So i've been doing great with my game until i came across detecting collisions.
for uiimageviews, i usually work with CGRectIntersectsRect(object.frame, object2.frame) for simpler games but this time it's not working.
i have a feeling its because of how i built my map: a tile based map using uiimageviews.

Code:
const int TILE_SIZE = 32;
const int WIDTH = 10;
const int HEIGHT = 15;

char map[15][10] = {
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 0, 0, 1, 1, 1},
    {1, 1, 1, 1, 1, 0, 2, 1, 1, 1},
    {1, 1, 1, 0, 0, 0, 1, 1, 1, 1},
    {1, 1, 1, 0, 0, 0, 1, 1, 1, 1},
    {1, 1, 1, 0, 1, 2, 2, 1, 1, 1},
    {1, 1, 1, 0, 0, 0, 0, 1, 1, 1},
    {1, 1, 1, 0, 0, 0, 0, 1, 1, 1},
    {1, 1, 1, 0, 0, 1, 0, 1, 1, 1},
    {1, 1, 1, 0, 0, 0, 0, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        greenBlock = [[UIImageView alloc] initWithFrame:CGRectMake(160.0f, 96.0f, 32.0f, 32.0f)];
        greenBlock.image = [UIImage imageNamed:@"GreenBlock.png"];
        [self addSubview:greenBlock];
        [greenBlock release];
        for (x = 0; x < WIDTH; x++) {
            for (y = 0; y < HEIGHT; y++) {
                if (map[y][x] == 1) {
                    imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(x * TILE_SIZE, y * TILE_SIZE, 32.0f, 32.0f)] autorelease];
                    imageView.image = [UIImage imageNamed:@"Wall.png"];
                    [self addSubview:imageView];
                }
                if (map[y][x] == 2) {
                    otherView = [[[UIImageView alloc] initWithFrame:CGRectMake(x * TILE_SIZE, y * TILE_SIZE, 32.0f, 32.0f)] autorelease];
                    otherView.image = [UIImage imageNamed:@"Hole.png"];
                    [self addSubview:otherView];
                }
            }
        }
    }
    return self;
}

after this i have a swipe detection for left/right/up and down and according to the direction detected, it moves the block accordingly.
in each detection i add a CGRectIntersectsRect to check a collision between the greenBlock.frame and the imageView.frame with NSLog (@"intersected!"); but nothing happens.

what am i doing wrong?

P.S. i KNOW the code above is really horrible. i was rushing to get to collision detection. before i had just images in the drawRect method to draw them with [object drawAtPoint:CGRectMake(blah blah)]; but i couldnt find a way to detect collision so i switched to what you see now: uiimageviews.

also a helpful note: im using CGAffineTransformMakeTranslation for the animation of the greenBlock moving in all directions.

ANY help would be much appreciated, thanks.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Detecting MBX/SGX, 3G vs 3GS Bersaelor 1 2,827 Aug 12, 2010 08:50 AM
Last Post: arekkusu
  Detecting hardware, which iPhone/iPod I'm running on? Jamie W 9 7,272 Apr 13, 2010 11:27 AM
Last Post: Frank C.