cocos2d 制作光晕特效

前端之家收集整理的这篇文章主要介绍了cocos2d 制作光晕特效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
*********************.h文件********************* @H_502_2@
#include "cocos2d.h" @H_502_2@
#include "HelloWorldScene.h" @H_502_2@

@H_502_2@
USING_NS_CC; @H_502_2@

@H_502_2@
class LightCCSprite : public CCSprite @H_502_2@
{ @H_502_2@
public: @H_502_2@
static CCSprite *spriteWithFile(const char * filename); @H_502_2@
virtual void draw(); @H_502_2@
}; @H_502_2@ @H_502_2@

@H_502_2@

@H_502_2@
****************.cpp文件****************** @H_502_2@
#include "LightCCSprite.h" @H_502_2@

@H_502_2@
CCSprite *LightCCSprite::spriteWithFile(const char * filename){ @H_502_2@

@H_502_2@
CCSprite *pobSprite = new LightCCSprite();//这里才会调用draw,而不是CCSprite的draw @H_502_2@

@H_502_2@
if (pobSprite && pobSprite->initWithFile(filename)) @H_502_2@
{ @H_502_2@
pobSprite->autorelease(); @H_502_2@
return pobSprite; @H_502_2@
} @H_502_2@
CC_SAFE_DELETE(pobSprite); @H_502_2@

@H_502_2@
return NULL; @H_502_2@
} @H_502_2@

@H_502_2@

@H_502_2@
void LightCCSprite::draw(){ @H_502_2@

@H_502_2@

@H_502_2@
//CCSprite::draw();//如果不取消的话如果两个lightCCSPrite对象只有一个会产生亮光 @H_502_2@

@H_502_2@
((HelloWorld*)getParent())->darknessLayer->begin(); @H_502_2@

@H_502_2@
CC_PROFILER_START_CATEGORY(kCCProfilerCategorySprit e,"CCSprite - draw"); @H_502_2@

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

@H_502_2@
CC_NODE_DRAW_SETUP(); @H_502_2@

@H_502_2@

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

@H_502_2@
#define kQuadSize sizeof(m_sQuad.bl) @H_502_2@

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

@H_502_2@

@H_502_2@
// ccGLEnableVertexAttribs( kCCVertexAttribFlag_PosColorTex ); @H_502_2@

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

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

@H_502_2@
// color @H_502_2@
diff = offsetof( ccV3F_C4B_T2F,colors); @H_502_2@
glVertexAttribPointer(kCCVertexAttrib_Color,4,GL_UNSIGNED_BYTE,GL_TRUE,(void*)(offset + diff)); @H_502_2@
glDrawArrays(GL_TRIANGLE_STRIP,4); @H_502_2@

@H_502_2@
glColorMask(1.0f,1.0f,1.0f); @H_502_2@
((HelloWorld*)getParent())->darknessLayer->end(); @H_502_2@
} @H_502_2@
********************调用处************************* @H_502_2@
CCSprite *sprite2=LightCCSprite::spriteWithFile("light.png");//继承ccsprite新建对象 @H_502_2@
sprite2->setPosition(ccp(100,200)); @H_502_2@
this->addChild(sprite2,2); @H_502_2@
sprite2->runAction(CCRepeatForever::create((CCActionInterval *)CCSequence::create(CCMoveBy::create(3.0f,ccp(300,0)),CCMoveBy::create(0.1f,ccp(-300,NULL))); @H_502_2@

@H_502_2@

@H_502_2@
darknessLayer = CCRenderTexture::create(size.width,size.height); @H_502_2@
darknessLayer->setPosition(ccp( size.width /2,size.height/2 )); @H_502_2@
this->addChild(darknessLayer,20); @H_502_2@
darknessLayer->clear(0,0.5f);//设置黑夜笼罩 @H_502_2@ @H_502_2@

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