粒子系统在游戏中模拟爆炸,光晕,燃烧,雨雪等效果,使我们游戏的画面更加绚丽,提高游戏的质量。cocos2d-x中有许许多多的特定效果粒子系统,我们选择其中模拟火把燃烧的粒子系统进行讲解。其他的粒子效果请大家自行了解,它们的用法都是差不多的。
(注意:我使用的cocos2d-x版本是2.0.4,系统是win7)下载地址
新建项目ParticleTest我们先导入燃烧粒子效果需要的图片,从D:\cocos2d-2.0-x-2.0.4\samples\TestCpp\Resources\Images(你的目录有可能不一样)找到fire.png复制到我们自己的项目中的Resources文件夹,然后在VS中右击项目选择添加->现有项,添加刚刚复制的图片到我们的项目ParticleTest即可。
修改HelloWorld.cpp文件的init()函数如下
bool HelloWorld::init()
{
bool bRet = false;
do
{
//
// super init first
//
CC_BREAK_IF(! CCLayer::init());
//
// add your codes below...
//
// 1. Add a menu item with "X" image, which is clicked to quit the program.
// Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
// Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
// Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
// Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);
//取得屏幕大小
CCSize size = CCDirector::sharedDirector()->getWinSize();
//新建一个粒子系统,类型为火
CCParticleSystem* m_emitter = CCParticleFire::create();
//设置粒子系统的渲染纹理
m_emitter->setTexture( CCTextureCache::sharedTextureCache()->addImage("fire.png") );
//设置粒子的位置
m_emitter->setPosition( ccp(size.width/2,size.height/2) );
//添加到布景中
addChild(m_emitter, 1);
bRet = true;
} while (0);
return bRet;
}
运行程序,我们会看到在屏幕的中央出现一个类似于火把燃烧的粒子系统。
由于是初学笔记,各种功能做的都比较简单,主要是为了初学者更好的学习用法,还请谅解。
最后祝愿每一个奋斗在路上的人早日实现梦想!