把一下代码复制到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,接下来就可以进行像素碰撞检测。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|