Cocos2d-x粒子效果

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "math.h"
#include "Box2D/Box2D.h"


#include "SimpleAudioEngine.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  
	HelloWorld();
	~HelloWorld();
    // there's no 'id' in cpp, so we recommand to return the exactly class pointer
    static cocos2d::CCScene* scene();

	virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
    virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
    virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
	void getLine(cocos2d::CCPoint pt);
	void getBoom(cocos2d::CCPoint pt);
	float x,y;
	cocos2d::CCParticleSmoke *smoke;
	cocos2d::CCParticleSun *sun;
	
	
    CREATE_FUNC(HelloWorld);
};

#endif  // __HELLOWORLD_SCENE_H__


#include "HelloWorldScene.h"

using namespace cocos2d;

	HelloWorld::HelloWorld()
	{
		smoke=CCParticleSmoke::create();
		sun=CCParticleSun::create();
		
		
	
	}
	HelloWorld::~HelloWorld()
	{
		smoke->setAutoRemoveOnFinish(true);
		sun->setAutoRemoveOnFinish(true);
		
	}

CCScene* HelloWorld::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
	
	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,false);
    bool bRet = false;
    do 
    {
        CC_BREAK_IF(! CCLayer::init());
        CCSprite* pSprite = CCSprite::create("background.png");
        CC_BREAK_IF(! pSprite);
        pSprite->setPosition(ccp(400, 240));
		this->addChild(pSprite, 0);

		
		sun->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("fire.png"),CCRectMake(0,0,32,32));  
		sun->setPosition(ccp(700,350));
		addChild(sun);
		

		/*CCParticleMeteor *meteor=CCParticleMeteor::create();
		meteor->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("fog.png"),CCRectMake(0,0,32,32));  
		meteor->setPosition(ccp(600,350));
		meteor->setLife(1);
		addChild(meteor);
		meteor->setAutoRemoveOnFinish(true);*/


		
		smoke->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("fog.png"),CCRectMake(0,0,32,32));  
		smoke->setPosition(ccp(600,150));
		smoke->setLife(1);
		addChild(smoke);
		


        bRet = true;
    } while (0);

    return bRet;
}
 bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
 {
	 x=pTouch->getLocation().x;
	 y=pTouch->getLocation().y;
	 CCLog("ccTouchBegan");
	 return true;
 }
 void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
 {
	 CCLog("ccTouchMoved");
	 getLine(pTouch->getLocation());
 }
 void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
 {
	 if(fabs(pTouch->getLocation().x-x)<20&&fabs(pTouch->getLocation().y-y)<20)
	 {
		 getBoom(pTouch->getLocation());
	 }
	 CCLog("ccTouchEnded");
 }

 void HelloWorld::getLine(CCPoint pt)
 {

	 
		//cocos2d::CCParticleSystemQuad *mSystem=CCParticleSystemQuad::particleWithFile("Particle.plist");
		cocos2d::CCParticleSystemQuad *mSystem=CCParticleSystemQuad::create("Particle.plist");
		//mSystem->initWithFile("Particle.plist");//plist文件可以通过例子编辑器获得
		mSystem->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("Particle.png")
        ,CCRectMake(0,0,32,32));//加载图片,第一个参数是纹理,第二个参数是选择图片上的位置
		 mSystem->setBlendAdditive(true);//这个调用必不可少
		mSystem->setPosition(pt);//设置位置
		mSystem->setDuration(0.3f);
		mSystem->setLife(0.5f);
		addChild(mSystem);
		//mSystem->release();
		//delete mSystem;
		//CC_SAFE_DELETE(mSystem);
		mSystem->setAutoRemoveOnFinish(true);
 }

 void HelloWorld::getBoom(CCPoint pt)
 {
	 	
		//cocos2d::CCParticleSystemQuad	*mSystem2 = CCParticleSystemQuad::particleWithFile("Boom.plist");
		cocos2d::CCParticleSystemQuad *mSystem2=CCParticleSystemQuad::create("Boom.plist");
		//mSystem2->initWithFile("Boom.plist");//plist文件可以通过例子编辑器获得
		mSystem2->setTextureWithRect(CCTextureCache::sharedTextureCache()->addImage("fire.png")
        ,CCRectMake(0,0,32,32));//加载图片,第一个参数是纹理,第二个参数是选择图片上的位置
		 mSystem2->setBlendAdditive(true);//这个调用必不可少
		mSystem2->setPosition(pt);//设置位置
		mSystem2->setDuration(0.05f);
		addChild(mSystem2);
		//mSystem2->release();
		//CC_SAFE_DELETE(mSystem2);
		mSystem2->setAutoRemoveOnFinish(true);
		
	
 }



源代码

http://download.csdn.net/detail/cp790621656/5754159

你可能感兴趣的:(Cocos2d-x粒子效果)