1: #ifndef _TEST_PARTICLESYSTEM
2: #define _TEST_PARTICLESYSTEM
3: //create by zgh149 2012/07/27
4:
5: #include "cocos2d.h" //include 需要的標頭檔
6: #include "SimpleAudioEngine.h" //include 需要的標頭檔
7:
8: using namespace cocos2d; //使用名稱空間
9: using namespace CocosDenshion; //使用名稱空間
10:
11: class Test_ParticleSystem : public CCLayerColor // 繼承 CCLayerColor 方便使用畫布顏色
12: {
13:
14: public:
15:
16: Test_ParticleSystem(void); //建構子
17:
18: ~Test_ParticleSystem(void); //解構子
19:
20: virtual bool init(); //初始化
21:
22: static CCScene* scene(); //建立scene
23:
24: LAYER_NODE_FUNC(Test_ParticleSystem); //建立layer
25:
26: bool sleep; //控制此流程的進行
27:
28: int gameFlow; //此流程的迴圈控制
29:
30: const static int GAMEFLOW1 =1;
31:
32: void Run(float dt);//主要迴圈
33:
34: int GF1Time;//流程時間控制
35:
36: void visit();//繪圖
37:
38: void nextScene(CCNode* sender);//下一個流程
39:
40:
41: //測試粒子效果
42: CCParticleSystem* explosion;
43: void explosionLoad();
44:
45:
46: protected:
47:
48: private:
49:
50: void ccTouchesEnded(CCSet* touches, CCEvent* event);//控制觸碰
51:
52: };
53:
54: #endif
55:
56: //create by zgh149
1: #include "Test_ParticleSystem.h"
2:
3: using namespace cocos2d;
4:
5: Test_ParticleSystem::Test_ParticleSystem(void)
6: {
7:
8: explosionLoad();
9:
10: //初始化各項數值
11: this->initWithColor(ccc4(0,0,0,255));
12: sleep = false;
13: gameFlow = 1;
14: GF1Time = 0;
15:
16: //加入schedule
17: this->schedule( schedule_selector(Test_ParticleSystem::Run),1); //加入schedule
18:
19:
20: }
21:
22: Test_ParticleSystem::~Test_ParticleSystem(void)
23: {
24:
25: CCLog("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
26: }
27:
28:
29: CCScene* Test_ParticleSystem::scene()
30: {
31:
32: CCScene* scene = NULL;
33:
34: do
35: {
36:
37: scene = CCScene::node();
38: CC_BREAK_IF(! scene);
39:
40:
41: Test_ParticleSystem *layer = Test_ParticleSystem::node();
42: CC_BREAK_IF(! layer);
43:
44:
45: scene->addChild(layer);
46: } while (0);
47:
48: return scene;
49: }
50:
51: void Test_ParticleSystem::Run(float dt)
52: {
53:
54: if(gameFlow==GAMEFLOW1)
55: {
56: GF1Time++;
57:
58:
59: }
60:
61: }
62:
63: void Test_ParticleSystem::ccTouchesEnded(CCSet* touches, CCEvent* event)
64: {
65:
66: CCTouch* touch = (CCTouch*)( touches->anyObject() );
67:
68: CCPoint location = touch->locationInView();
69:
70: location = CCDirector::sharedDirector()->convertToGL(location);
71:
72: CCLog("convertToGLXY x:%f, y:%f", location.x, location.y);
73:
74:
75: }
76:
77:
78: bool Test_ParticleSystem::init()
79: {
80: bool bRet = false;
81: do
82: {
83:
84: this->setTouchEnabled(true);
85:
86:
87: bRet = true;
88:
89: } while (0);
90:
91: return bRet;
92: }
93:
94: void Test_ParticleSystem::visit()
95: {
96: if(gameFlow==GAMEFLOW1)
97: {
98: this->draw();
99: explosion->visit();
100: }
101:
102: }
103:
104: void Test_ParticleSystem::nextScene(CCNode* sender)
105: {
106:
107: bool tempa = CocosDenshion::SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying();
108:
109: if(tempa)
110: {
111:
112: CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic(true);
113: }
114:
115: }
116:
117:
118:
119: void Test_ParticleSystem::explosionLoad()
120: {
121:
122: explosion = CCParticleFireworks::create();
123: explosion->retain();
124: this->addChild(explosion, 10);
125:
126: explosion->setTexture( CCTextureCache::sharedTextureCache()->addImage("stars.png") );
127:
128:
129: }
1: void Test_ParticleSystem::explosionLoad()
2: {
3:
4: explosion = CCParticleFireworks::create();
5: explosion->retain();
6: this->addChild(explosion, 10);
7:
8: explosion->setTexture( CCTextureCache::sharedTextureCache()->addImage("stars.png") );
9:
10: explosion->setStartSize(1);//開始尺寸
11:
12: explosion->setEndSize(20); //結束尺寸
13:
14: explosion->setDuration(3); //週期
15:
16: explosion->setStartColor(ccc4f(100,255,100,255));//開始顏色
17:
18: explosion->setEndColor(ccc4f(100,255,0,255));//結束顏色
19:
20: explosion->setPosition(ccp(480/2,0));//座標
21: }