0
votes

im writing a game to find the differences between 2 images. i created a subclass of CCSprite, Spot. firstly i tried to create small images and add itself according to it's position, but later i found the position is hard to determine, since it's hard to avoid offset of 1 or 2 pixels.

then i tried to make the Spot the same size as the image, with the other part transparent. but I still need to find out the 'hotspot' of finger tap. but when i use CGRectContainsPoint([self boundingBox], touchLocation), it's actually the whole image.

so is there any other way to do this? like content.size or self.size, and make a CGRect out of it's non-transparent part? Thank you.

2
I've tried [self boundingBox].size.width, self.texture.contentSize.width and self.contentSize.width, they are all the same as the whole image size. how can i only get the size of non-transparent area?OMGPOP
hi, the question become, how to determine whether the pixel in touchLocation is transparent. (if it is not, then the touchLocation is the 'hot spot'. should be easy if you have experience in colors etc. i dont have. so please help me (with some code) Thank youOMGPOP
Well, i still need to find out the CGRect of non-transparent area. (for other usage) thank you!OMGPOP

2 Answers

1
votes

I figured it out now. here is my code: (it's actually quite simple

-(void) findRect:(NSString*) fn {
//the origin of mTex is top left
//the origin of CGRect is top left, in the coordinate system inside the image
int topLeftX = 0;
int topLeftY = 0;
for (int i = 0; i < image_width; i += 10) {
    for (int j = 0; j < image_height; j += 10) {
        if (([mTex pixelAt:ccp(i, j)].a & 0xFF) != 0) {
            topLeftX = i;
            topLeftY = j;
            goto outer;
        }
    }
}
outer:;
int topRightX = 0;
for (int i = topLeftX; i < image_width; i += 10) {
    if (([mTex pixelAt:ccp(i, topLeftY)].a & 0xFF) == 0) {
        topRightX = i;
        break;
    }
}
if (topRightX == 0) {
    topRightX = image_width - 1;
}

int bottomLeftY = 0;
for (int i = topLeftY; i < image_height; i += 10) {
    if (([mTex pixelAt:ccp(topLeftX, i)].a & 0xFF) == 0) {
        bottomLeftY = i;
        break;
    }
}
if (bottomLeftY == 0) {
    bottomLeftY = image_height - 1;
}
areaRect = CGRectMake(topLeftX, topLeftY, topRightX - topLeftX, bottomLeftY - topLeftY);
}
0
votes

You would have to look at the texture data. In essence you're looking for pixel-perfect collision detection algorithm. See this post for more info:

http://www.cocos2d-iphone.org/forum/topic/1188