February 23, 2016

Deploy Particle2dx system for Cocos2d-x games in Windows OS [Part 3]: Import Particle to Cocos2d-x game.

Hi everyone! This is the final post of series "Deploy Particle2dx system for Cocos2d-x games in Windows OS". Here are my previous posts: Part 1, Part 2.

In part 2, I did create 2 files: "Fire_Wheel.png" for the image which will be used to display a chain of movement in screen and "Fire_Wheel.plist" to control the direction of movement. In this post, I will continue to use those files to import to Cocos2d-x project in Visual Studio.

Step 1: I have a character in png format with file name "Fighter.png". I will add the wheel under his legs.
A character will be added to game.
Fig 1: A character will be added to game.
Step 2: Put that "Fighter.png", "Fire_Wheel.png" and "Fire_Wheel.plist" to the "Resources" folder of Cocos2d-x project.
Capture 3 files to Resources project.
Fig 2. Capture 3 files to Resources project.
Step 3: Open the "HelloWorldScene.cpp", copy these code lines to the bottom line of "init()" method:
Add code to init methods.
Fig 3. Add code to init methods.
In this code, first, I add "Figher.png" to a Sprite object and add it to middle Y and right X in screen. It will be moved from Right to Left. After that, I create a "ParticleSystemQuad" object to scan the plist file to get all information of the "Fire_Wheel" particle. Two "MoveTo" objects will perform the move action from right to left for both Sprite and ParticleSystemQuad object.

Here is the code in text:
Sprite* yumikiFighter = Sprite::create("Fighter.png");

yumikiFighter->setAnchorPoint(Vec2(0.5, 0.5));
yumikiFighter->setPosition(Vec2(visibleSize.width + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(yumikiFighter, 0);
ParticleSystemQuad* preParticle = ParticleSystemQuad::create("Fire_Wheel.plist");
preParticle->setScale(0.5);
preParticle->setPosition(Vec2(yumikiFighter->getPositionX() + yumikiFighter->getContentSize().width / 2 + origin.x
, yumikiFighter->getPositionY() - yumikiFighter->getContentSize().height / 2 + origin.y));
this->addChild(preParticle);
MoveTo* moveToFighter = MoveTo::create(5, Vec2(0 + origin.x, yumikiFighter->getPositionY() + origin.y));
yumikiFighter->runAction(moveToFighter);
MoveTo* moveToParticle = MoveTo::create(5, Vec2(0 + origin.x, preParticle->getPositionY() + origin.y));
preParticle->runAction(moveToParticle);
After adding code, run the project and you will see the Fighter will move with the particle:
Sprite object will move with particle effect.
Fig 4. Sprite object will move with particle effect.
If you compare with the Sprite object without Particle effect, you can see it will be less attractive than the one with particle effect:
Sprite object will move without particle effect.
Fig 5. Sprite object will move without particle effect.
 So that's all for this topic. I hope that you are able to make your own particle well. 

If you have any questions or concerns, please post your comment here, I will take a look and response ASAP. And if you are interesting in my posts, please follow and like me on my posts. Thank you!

Yumiki.

No comments :

Post a Comment