*********************.h文件*********************
#include "cocos2d.h"
#include "HelloWorldScene.h"
USING_NS_CC;
class LightCCSprite : public CCSprite
{
public:
static CCSprite *spriteWithFile(const char * filename);
virtual void draw();
};
****************.cpp文件******************
#include "LightCCSprite.h"
CCSprite *LightCCSprite::spriteWithFile(const char * filename){
CCSprite *pobSprite = new LightCCSprite();//这里才会调用draw,而不是CCSprite的draw
if (pobSprite && pobSprite->initWithFile(filename))
{
pobSprite->autorelease();
return pobSprite;
}
CC_SAFE_DELETE(pobSprite);
return NULL;
}
void LightCCSprite::draw(){
//CCSprite::draw();//如果不取消的话如果两个lightCCSPrite对象只有一个会产生亮光
((HelloWorld*)getParent())->darknessLayer->begin();
CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprit
e,"CCSprite - draw");
glBlendFunc(GL_ZERO,GL_ONE_MINUS_SRC_ALPHA);
#define kQuadSize sizeof(m_sQuad.bl)
if (m_pobTexture!= NULL)
{
glBindTexture(GL_TEXTURE_2D,m_pobTexture->getName());
}
else
{
glBindTexture(GL_TEXTURE_2D,0);
}
long offset = (long)&m_sQuad;
glDrawArrays(GL_TRIANGLE_STRIP,4);
glColorMask(1.0f,1.0f,1.0f);
((HelloWorld*)getParent())->darknessLayer->end();
}
********************调用处*************************
CCSprite *sprite2=LightCCSprite::spriteWithFile("light.png");//继承ccsprite新建对象
sprite2->setPosition(ccp(100,200));
this->addChild(sprite2,2);
sprite2->runAction(CCRepeatForever::create((CCActionInterval *)CCSequence::create(CCMoveBy::create(3.0f,ccp(300,0)),CCMoveBy::create(0.1f,ccp(-300,NULL)));
darknessLayer = CCRenderTexture::create(size.width,size.height);
darknessLayer->setPosition(ccp( size.width /2,size.height/2 ));
this->addChild(darknessLayer,20);
darknessLayer->clear(0,0.5f);//设置黑夜笼罩
*********************最终效果************************