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

前端之家收集整理的这篇文章主要介绍了Cocos2D游戏之旅(六):流星、烟花、火焰、下雪、下雨粒子特效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

晓石头的博客

邮箱:178673693@qq.com

转载请注明出处,原文链接http://www.jb51.cc/article/p-diecaqxz-vx.html



效果演示

一、整体实现

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

二、单个详解

流星:

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://www.jb51.cc/article/p-bfwzzrss-nk.html

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

猜你在找的Cocos2d-x相关文章