获取Cocosdx精灵的像素点的RGBA

前端之家收集整理的这篇文章主要介绍了获取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,接下来就可以进行像素碰撞检测。

原文链接:https://www.f2er.com/cocos2dx/346195.html

猜你在找的Cocos2d-x相关文章