我们在写游戏的过程中为了显示爆炸或者碰撞的效果,需要以一定的形式进行表现,这时就可能需要
粒子
效果。例如碰撞时的碎片,子弹的运动过程中可能裹着一团火焰等。
头文件:MyParticleScence.h
CCParticleSmoke是类似冒烟的效果,下面看代码:
头文件:MyParticleScence.h
#pragma
once
#ifndef
__MyParticleScence_H__
#define
__MyParticleScence_H__
#include
"cocos2d.h"
using
namespace
cocos2d;
class
MyParticleScence :
public
CCLayer
{
public
:
static
CCScene* createScene();
MyParticleScence(
void
);
~MyParticleScence(
void
);
virtual
bool
init();
CREATE_FUNC(MyParticleScence);
private
:
CCParticleSystem* particlSystem;
CCSprite* backgroundSprite;
};
#endif
MyParticleScence.cpp
#include
"MyParticleScence.h"
MyParticleScence::MyParticleScence(
void
)
{
}
MyParticleScence::~MyParticleScence(
void
)
{
}
CCScene* MyParticleScence::createScene(){
CCScene* particleScene = NULL;
do
{
particleScene = CCScene::create();
CC_BREAK_IF(!particleScene);
MyParticleScence* myParticleScene = MyParticleScence::create();
CC_BREAK_IF(!myParticleScene);
particleScene->addChild(myParticleScene);
}
while
(0);
return
particleScene;
}
bool
MyParticleScence::init(){
bool
bRet =
false
;
do
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
backgroundSprite = CCSprite::create(
"background3.jpg"
);
CC_BREAK_IF(!backgroundSprite);
backgroundSprite->setPosition(ccp( screenSize.width/2 , screenSize.height/2 ));
this
->addChild(backgroundSprite);
//下?面?的?两?中D效果?都?可以?使1用?
/*particlSystem = CCParticleFire::create();*/
particlSystem = CCParticleSmoke::create();
CC_BREAK_IF(!particlSystem);
particlSystem->setTexture(CCTextureCache::sharedTextureCache()->addImage(
"loading_04.png"
));
//可以?设置?粒子效果?在精?灵中D的?位?置?
particlSystem->setPosition(backgroundSprite->getContentSize().width/3,backgroundSprite->getContentSize().height/3);
backgroundSprite->addChild(particlSystem);
bRet =
true
;
}
while
(0);
return
bRet;
}
在这里
particlSystem
也可以不显示在CCSprite上,而直接加载Layer上。
loading_04.png就形成一种冒烟的效果,然后不断的屏幕上展示,每一种粒子效果都有一个展示的粒子数或者其他各项参数,这些都体现在它们的封装类中。
一些粒子效果:
-- CCParticleExplosion (爆炸粒子效果)
-- CCParticleFireworks (烟花粒子效果)
-- CCParticleFire (火焰粒子效果)
-- CCParticleFlower (花束粒子效果)
-- CCParticleGalaxy (星系粒子效果)
-- CCParticleMeteor (流星粒子效果)
-- CCParticleSpiral (漩涡粒子效果)
-- CCParticleSnow (雪粒子效果)
-- CCParticleSmoke (烟粒子效果)
-- CCParticleSun (太阳粒子效果)
-- CCParticleRain (雨粒子效果)
源代码链接:http://pan.baidu.com/s/1ABrGZ