Cocos2D游戏之旅(六):流星、烟花、火焰、下雪、下雨粒子特效

晓石头的博客

邮箱:[email protected]

转载请注明出处,原文链接:http://blog.csdn.net/qiulanzhu/article/details/47910781



效果演示

一、整体实现

点击每一个菜单选项,显示相应的的特效。分别对应:流星、烟花、火焰、下雪、下雨。

 

二、单个详解

流星:

1、粒子系统 CCParticleMeteor创建流星效果

2、定时器移动位置产生坠落效果

void HelloWorld::fallingStar(Ref* ref){	
	static int isSchedule = 0;		//控制scheduleUpdate只注册一次

	/* 创建背景精灵 */
	Sprite* bkSnow = Sprite::create("bkSky.jpg");
	bkSnow->setPosition(Point(size.width / 2, size.height / 2));
	this->addChild(bkSnow);

	//流星效果  
	shootingStar = CCParticleMeteor::create();
	shootingStar->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
	shootingStar->setPosition(Point(size.width / 2, size.height / 2));
	addChild(shootingStar);
	if (0 == isSchedule)
	{
		isSchedule++;
		this->scheduleUpdate();
	}

	return;
}

void HelloWorld::update(float t)
{
	/* 流星坠落效果 */
	shootingStar->setPositionX(shootingStar->getPositionX() + 4);	
	shootingStar->setPositionY(shootingStar->getPositionY() - 2);
	
	if (shootingStar->getPositionX() > 480 || shootingStar->getPositionY() < 0)
	{
		shootingStar->setPositionX(0);
		shootingStar->setPositionY(size.height);
	}
}


==================================================================

烟花:

粒子系统 CCParticleFireworks创建烟花效果

void HelloWorld::fireBoom(Ref* ref){

	/* 创建背景精灵 */
	Sprite* bkSnow = Sprite::create("bkSky.jpg");
	bkSnow->setPosition(Point(size.width / 2, size.height / 2));
	this->addChild(bkSnow);

	//烟花效果  
	CCParticleSystem* particleSystem1 = CCParticleFireworks::create();
	particleSystem1->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
	particleSystem1->setPosition(Point(80, 0));
	addChild(particleSystem1);

	CCParticleSystem* particleSystem2 = CCParticleFireworks::create();
	particleSystem2->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
	particleSystem2->setPosition(Point(240, 0));
	addChild(particleSystem2);

	CCParticleSystem* particleSystem3 = CCParticleFireworks::create();
	particleSystem3->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
	particleSystem3->setPosition(Point(400, 0));
	addChild(particleSystem3);

	return;
}

==================================================================

火焰:

粒子系统 CCParticleFire创建火焰效果

void HelloWorld::flame(Ref* ref){
	/* 创建背景精灵 */
	Sprite* bkSnow = Sprite::create("bkFire.jpg");
	bkSnow->setPosition(Point(size.width / 2, size.height / 2));
	this->addChild(bkSnow);
	//火焰效果  
	CCParticleSystem* particleSystem = CCParticleFire::create();
	particleSystem->setPosition(Point(250, 130));
	particleSystem->setTexture(CCTextureCache::sharedTextureCache()->addImage("fire.png"));
	addChild(particleSystem);

	return;
}

==================================================================

雪花:

粒子系统 CCParticleSnow创建雪花效果。

void HelloWorld::snow(Ref* ref){
	/* 创建背景精灵 */
	Sprite* bkSnow = Sprite::create("bkSnow.png");
	bkSnow->setPosition(Point(size.width / 2, size.height / 2));
	this->addChild(bkSnow);

	/* 雪花效果 */
	CCParticleSystem* particleSystem = CCParticleSnow::create();	//创建粒子系统
	particleSystem->setTexture(CCTextureCache::sharedTextureCache()->addImage("snow.png"));	//设置纹理
	particleSystem->setSpeed(5.0f);		//下雪的速度
	this->addChild(particleSystem);

	return;
}

==================================================================

下雨:

粒子系统 CCParticleRain创建下雨效果。

void HelloWorld::rain(Ref* ref){
	/* 创建背景精灵 */
	Sprite* bkSnow = Sprite::create("bkRain.jpg");
	bkSnow->setPosition(Point(size.width / 2, size.height / 2));
	this->addChild(bkSnow);

	/* 下雨效果 */
	CCParticleSystem* particleSystem = CCParticleRain::create();
	particleSystem->setTexture(CCTextureCache::sharedTextureCache()->addImage("snow.png"));
	particleSystem->setSpeed(300.0f);
	addChild(particleSystem);

	return;
}

更多的粒子效果,移步@song_hui_xiang的博客:http://blog.csdn.net/song_hui_xiang/article/details/8712240

 

源代码免积分下载地址:http://download.csdn.net/detail/qiulanzhu/9039203





你可能感兴趣的:(Cocos2D-X游戏之旅,Cocos2D游戏之旅)