问题描述
|
我正在编写一个游戏来查找2张图像之间的差异。我创建了CCSprite Spot的子类。首先,我尝试创建小图像并根据其位置添加自身,但后来我发现该位置很难确定,因为很难避免1或2像素的偏移。
然后,我尝试使Spot与图像大小相同,而另一部分透明。但我仍然需要找出手指点击的“热点”。但是当我使用CGRectContainsPoint([self boundingBox],touchLocation)时,它实际上是整个图像。
所以还有其他方法吗?例如content.size或self.size,并从其非透明部分中制作一个CGRect?
谢谢。
解决方法
我现在想通了。这是我的代码:(实际上很简单
-(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);
}
, 您将不得不查看纹理数据。本质上,您正在寻找像素完美的碰撞检测算法。有关更多信息,请参见此帖子:
http://www.cocos2d-iphone.org/forum/topic/1188