cocos2d content.size,boundingBox和size

时间:2011-05-28 08:10:50

标签: iphone ios cocos2d-iphone size

我正在编写游戏以找出2张图片之间的差异。我创建了一个CCSprite,Spot的子类。首先,我试图创建小图像,并根据它的位置添加自己,但后来我发现位置很难确定,因为很难避免1或2像素的偏移。

然后我尝试使Spot与图像尺寸相同,另一部分透明。但我仍然需要找出手指敲击的“热点”。但是当我使用CGRectContainsPoint([self boundingBox],touchLocation)时,它实际上是整个图像。

有没有其他方法可以做到这一点?喜欢content.size或self.size,并从它的非透明部分制作一个CGRect? 谢谢。

2 个答案:

答案 0 :(得分:1)

我现在明白了。这是我的代码:(实际上非常简单

-(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);
}

答案 1 :(得分:0)

您必须查看纹理数据。从本质上讲,您正在寻找像素完美的碰撞检测算法。有关详细信息,请参阅此帖子:

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