Tuesday, December 29, 2015

Custom Cocos2d-x motion streak

First I read the post of this place:

https://www.planarbox.com/blog/cocos2d-x/improved-cocos2d-x-motionstreak/

Ok lets think about this, when you create a motion streak node in cocos2d-x you need to use something like this, I take this from the web site of Planar box. So you  need to create a schedule update to make it work, but bassicaly the MotionStreak by itself have that so making some little changes you can have the amazing effect working with less code.


 bool MyLayer::init()  
 {  
   /* ... initialize layer ... */  
   //add a sprite  
   _sprite = Sprite::create("circle.png");  
   _sprite->setPosition(100,100);  
   addChild(_sprite);  
   //add a MotionStreak  
   _streak = MotionStreak::create(0.15f, 5, 50, Color3B::RED, "circle.png");  
   addChild(_streak);  
   //schedule the update() function  
   scheduleUpdate();  
   return true;  
 }  
 void MyLayer::update(float dt)  
 {  
   _streak->setPosition(_sprite->getPosition());  
 }  
//Of course this was taken from this website
//https://www.planarbox.com/blog/cocos2d-x/improved-cocos2d-x-motionstreak/

Can made it more easier?

Yes. this is the example also taken from planar box.

     //add a MotionStreak  
     auto streak = MotionStreak::create(0.15f, 5, 50, Color3B::RED, "circle.png");  
     streak->setFastMode(true); //use this if you have a complex scene  
     //follow the circle sprite's center position  
     streak->followNode(circle, Vec2(circle->getContentSize().width * 0.5f, circle->getContentSize().height * 0.5f));  
     addChild(streak);  

How to use it ?

First you need to have created a new cocos2d-x project
change this files CCMotionStreak.h & CCMotionStreak.cpp
they must be in a place like here
D:\AndroidApps\YouGameProjectFolder\cocos2d\cocos\2d

And replace it with this files that you can find here.
https://gist.github.com/OscarLeif/507232e0d4dbb486e8e3

Thanks to PlanarBox to the examplesI was able to create an updated version.

2 comments:

  1. Replies
    1. Sorry I didn't get the notification.
      Please tell me what version of cocos2d-x are you using ?
      I'm gonna check what happened.

      Delete