i write a GlowSprite:
//GlowSprite.h
#include "cocos2d.h"
USING_NS_CC;
class GlowSprite : public CCSprite {
private:
CCSprite* _glow;
CCSize _size;
CCSequence* _seq;
public:
CREATE_FUNC(GlowSprite);
bool init();
CCSprite* addGlowEffect(const ccColor3B& colour, const float scaleFactor, const GLubyte opacity);
void removeGlowEffect();
void playEffect(bool open);
};
//GlowSprite.cpp
#include "GlowSprite.h"
bool GlowSprite::init()
{
initWithFile("flag.png");
_glow = addGlowEffect(ccc3(111,222,12), 0.6, 200);
this->addChild(_glow, -1);
CCScaleBy *ccSB = CCScaleBy::create(0.5f, 0.6, 0.6);
// Run some animation which scales a bit the glow
CCSequence* s1 = CCSequence::createWithTwoActions(ccSB, ccSB->reverse());
CCRepeat* r = CCRepeat::create(s1, 5);
_seq = CCSequence::createWithTwoActions(r, CCCallFuncN::create(this, callfuncN_selector(GlowSprite::removeGlowEffect)));
_glow->setVisible(false);
_glow->retain();
_seq->retain();
return true;
}
void GlowSprite::playEffect(bool open)
{
_glow->setVisible(open);
if (open) {
_glow->runAction(_seq);
}
else{
_glow->stopAllActions();
}
}
CCSprite* GlowSprite::addGlowEffect(const ccColor3B& colour, const float scaleFactor, const GLubyte opacity)
{
if(this == NULL)
return NULL;
_size = this->getContentSize();
CCPoint pos = ccp(_size.width / 2, _size.height / 2);
CCRenderTexture *rt = CCRenderTexture::create(_size.width, _size.height);
rt->beginWithClear(255, 255, 255, 60);
rt->visit();
rt->end();
CCTexture2D *tex = rt->getSprite()->getTexture();
CCSprite* glowSprite = CCSprite::createWithTexture(tex);
glowSprite->setColor(colour);
glowSprite->setOpacity(opacity);
glowSprite->setPosition(pos);
glowSprite->setRotation(this->getRotation());
_ccBlendFunc f = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA};
glowSprite->setBlendFunc(f);
return glowSprite;
}
and in HelloWorldScene.cpp
int lastGlowIdx = -1;
void HelloWorld::displayGlow()
{
int idx = getRand(FLAG_NUM);
CCLog("idx: %d", idx);
if (lastGlowIdx != idx) {
//if
if(lastGlowIdx >= 0)
glows[lastGlowIdx].playEffect(false);
lastGlowIdx = idx;
glows[lastGlowIdx].playEffect(true);
}
}
in function init of HelloWorldScene.cpp
CCLabelBMFont* createLb = CCLabelBMFont::create("create", "konqa32.fnt");
CCMenuItemLabel* createItem = CCMenuItemLabel::create(createLb, this, menu_selector(HelloWorld::displayGlow));
createItem->setPosition(ccp(350, 20));
_menu = CCMenu::create(createItem, NULL);
this->addChild(_menu);
_menu->setPosition(CCPointZero);
for (int i=0; isetPosition(getPos());
this->addChild(gl);
gl->retain();
glows[i] = * gl;
};
i hope these will help you.
Jonathan Yeung wrote:
I think I may not catch up what you want to say.
>