iphone - 如何获取CCSprite中特定像素的RGBA颜色

标签 iphone objective-c cocos2d-iphone ccsprite

我正在使用 cocos2d for iPhone v1.0.1 库为 iPhone 编写游戏。为了让我的游戏正常工作,我需要在知道坐标时检查 CCSprite 中特定像素的颜色。我一直在寻找解决方案两天,但我没有找到任何工作。也许有人以前做过这个并且知道怎么做?

如果这更容易的话,对我来说另一种可能性是从 CCSprite 创建一个 UIImage...

问候, jarektb

最佳答案

显然不能直接访问包含 Sprite 的颜色缓冲区。但是,您可以将 Sprite 绘制到 CCRenderTexture 并从那里读取像素。

location = ccp(x * CC_CONTENT_SCALE_FACTOR(), y * CC_CONTENT_SCALE_FACTOR());

UInt8 data[4];

CCRenderTexture* renderTexture = [[CCRenderTexture alloc] initWithWidth:sprite.boundingBox.size.width * CC_CONTENT_SCALE_FACTOR() 
                                                                 height:sprite.boundingBox.size.height * CC_CONTENT_SCALE_FACTOR() 
                                                            pixelFormat:kCCTexture2DPixelFormat_RGBA8888];

[renderTexture begin];
[sprite draw];

glReadPixels((GLint)location.x,(GLint)location.y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);

[renderTexture end];
[renderTexture release];

NSLog(@"R: %d, G: %d, B: %d, A: %d", data[0], data[1], data[2], data[3]);

如果您使用的是 Retina 显示器,则必须考虑内容比例因子。

该解决方案也可以很容易地转化为 CCSprite 类别或子类。

这似乎是一个老话题,但我在这里发布了答案,因为这是我刚才遇到同样困境时在谷歌上的第一次点击。

关于iphone - 如何获取CCSprite中特定像素的RGBA颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685517/

相关文章:

iphone - 在iPhone中使用sqlite数据库时的内存管理

iphone - GEO 提醒启动应用程序

objective-c - 如何获得旋转的 CCNode 的角坐标?

iphone - UIView setHidden 不工作

iphone - Cocos2d 中的 CCAction 帮助

objective-c - Cocos2d如何创建进度条(加载场景)

iphone - 理解属性访问概念时遇到问题

iphone - iOS 核心数据对象的唯一标识符?

objective-c - Uncrustify:哪个开关或设置影响@interface 和@protocol 的{?

iPhone短动画: video or image sequence?