cocos2d-xで星を散らす演出を作る
概要
星の画像(スプライト)を中心から360度ランダムな方向に散らせるエフェクトを作る。
環境
windows8.1
cocos2d-x v2.2.3
手順
init関数にスケジュールを設定しておく。
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } // 0.1秒ごとにstarWorldEffect関数を実行する this->schedule(schedule_selector(HelloWorld::starWorldEffect), 0.1f); ・・・ }
starWorldEffect関数には以下のソースコードを書いておく。
void HelloWorld::starWorldEffect() { CCSize winSize = CCDirector::sharedDirector()->getWinSize(); // 星をランダムに配置する int width = -100 + (rand() % ((int)winSize.width + 100)); int height = -100 + (rand() % ((int)winSize.height + 100)); CCSprite* pSprite = CCSprite::create("star.png"); pSprite->setPosition(ccp(winSize.width/2, winSize.height/2)); pSprite->setOpacity(128); this->addChild(pSprite); // 星をランダムな方向へ透過させながら移動させる CCMoveTo* actionMove = CCMoveTo::create(1.0f, ccp(width, height)); CCCallFuncN* actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)); pSprite->runAction(CCSequence::create(actionMove, actionMoveDone, NULL)); CCFadeOut* actionFadeOut = CCFadeOut::create(1.0f); pSprite->runAction(actionFadeOut); }
このままでは画像スプライトが溜まっていく一方なので、アニメーションが終了したら削除する。
void HelloWorld::spriteMoveFinished(CCNode* sender) { CCSprite* pSprite = (CCSprite*)sender; this->removeChild(pSprite, true); }
こんな感じのエフェクトになります。
何かアイテムを手に入れた時に使えそうですね。