获取Cocosdx精灵的像素点的RGBA

获取Cocosdx精灵的像素点的RGBA,第1张

概述把一下代码复制到CCImage.h中: ccColor4B getColor4B(float x,float y)   {        ccColor4B color = { 0, 0, 0, 0 };       if(x<0.f || y<0.f || x>getWidth() || y>getHeight())     {      CCLOG("cocos2d: CCImage::get

把一下代码复制到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,接下来就可以进行像素碰撞检测。

总结

以上是内存溢出为你收集整理的获取Cocosdx精灵的像素点的RGBA全部内容,希望文章能够帮你解决获取Cocosdx精灵的像素点的RGBA所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1010636.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存