获取Cocosdx精灵的像素点的RGBA

把一下代码复制到CCImage.h中:

ccColor4B getColor4B(float x,float y)
{

ccColor4B color = { 0,0 };
if(x<0.f || y<0.f || x>getWidth() || y>getHeight())
{
CCLOG("cocos2d: CCImage::getColor4B(%2.f,%2.f) x or y is not in range of the image. W:%d,H:%d",
x,y,getWidth(),getHeight());
return color;
}
int ix = (int)x - 1;
int iy = (int)y - 1;
unsigned char* pos = m_pData;
pos += (iy*getWidth() + ix) * 4;
color.r = *(pos++);
color.g = *(pos++);
color.b = *(pos++);
color.a = *(pos++);
return color;
};

ccColor4F getColor4F(float x,float y)
{
return ccc4FFromccc4B(getColor4B(x,y));
};

在场景中增加帧函数:

void HelloWorld::update(float dt)
{
//this->setPosition(m_orignPoint - m_player->m_sprite->getPosition());

if(g_player->getPlayerBounding().intersectsRect(g_police->getPoliceBounding())){

//获取玩家的像素。
CCSprite*newSprite=CCSprite::createWithSpriteFrame(g_police->getPoliceSprite()->displayFrame());

CCRenderTexture* render=CCRenderTexture::create(g_police->getPoliceBounding().size.width,
g_police->getPoliceBounding().size.height,kCCTexture2DPixelFormat_RGBA8888);

render->begin();
newSprite->visit();
render->end();
CCImage* image=render->newCCImage();

ccColor4B color=image->getColor4B(10,20);
if(color.a!=0){CCLOG("bu deng yu kong:%d",color.a);}

}
}

到此,完成了获取精灵像素的RGBA,接下来就可以进行像素碰撞检测。

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...