iphone - cocos2d content.size、boundingBox 和 size

标签 iphone ios cocos2d-iphone size

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

然后我尝试使 Spot 与图像大小相同,其他部分透明。但我仍然需要找出手指点击的“热点”。但是当我使用 CGRectContainsPoint([selfboundingBox], 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);
}

关于iphone - cocos2d content.size、boundingBox 和 size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6160473/

相关文章:

ios - 使用 Storyboard发送用户查看 Controller

android - 在 cocos2dx 中加载资源,它适用于 android 和 iPhone

iphone - 在 iPhone 应用程序中播放视频库中的视频

ios - 将应用程序更新到 iOS 7 时,奇怪的 Storyboard会推送 segue。

ios - 显示在 MBProgressHUD 顶部的 UITableView 标题

ios - 在展开 UITableVewCell 自定义单元格类的 Optional 值时意外发现 nil

cocos2d-iphone - 如何让 iAD 方法被调用

ios - Xcode Undefined Symbols For Architecture 无法在 64 位模拟器中运行

ios - 如何为 iOS 应用程序测试设置仅 IPv6 网络?

iphone - 我的应用程序开始颠倒