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>制作光晕特效

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...