cocos2d 制作光晕特效

前端之家收集整理的这篇文章主要介绍了cocos2d 制作光晕特效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
*********************.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");

CCAssert(!m_pobBatchNode,"If CCSprite is being rendered by CCSpriteBatchNode,CCSprite#draw SHOULD NOT be called");

CC_NODE_DRAW_SETUP();


//glClear(GL_COLOR_BUFFER_BIT);
glBlendFunc(GL_ZERO,GL_ONE_MINUS_SRC_ALPHA);
glColorMask(0.0f,0.0f,1.0f);//关键句

#define kQuadSize sizeof(m_sQuad.bl)

if (m_pobTexture!= NULL)
{
glBindTexture(GL_TEXTURE_2D,m_pobTexture->getName());
}
else
{
glBindTexture(GL_TEXTURE_2D,0);
}


// ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex );

long offset = (long)&m_sQuad;
// vertex
int diff = offsetof( ccV3F_C4B_T2F,vertices);
glVertexAttribPointer(kCCVertexAttrib_Position,3,GL_FLOAT,GL_FALSE,kQuadSize,(void*) (offset + diff));

// texCoods
diff = offsetof( ccV3F_C4B_T2F,texCoords);
glVertexAttribPointer(kCCVertexAttrib_TexCoords,2,(void*)(offset + diff));

// color
diff = offsetof( ccV3F_C4B_T2F,colors);
glVertexAttribPointer(kCCVertexAttrib_Color,4,GL_UNSIGNED_BYTE,GL_TRUE,(void*)(offset + diff));
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);//设置黑夜笼罩
*********************最终效果************************

cocos2d <wbr>制作光晕特效

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