1. 程式人生 > >cocos2dx中實現拖尾的效果

cocos2dx中實現拖尾的效果

之前看到拖尾效果感覺挺酷炫的,但是網上很多教程說得並明瞭,現在我來個簡單粗暴的,現在做了一個很菜的效果大家湊合看看,廢話不多說,直接上程式碼:

首先建立一個主角:

        bullt = CCSprite::create("blood.png");  
bullt->setPosition(ccp(100,110));  
this->addChild(bullt,2); 


建立拖尾效果並且新增到螢幕:

        m_streak = CCMotionStreak::create(2, 10,20, ccRED, "blood.png");  
m_streak->setPosition(ccp(100,110));  
this->addChild(m_streak,1);

新增漸變顏色:

//  //建立一個無限迴圈的動畫序列,動畫序列為7個變色動畫,哪個結點使用它就會不斷的變色。  
 CCActionInterval *colorAction = CCRepeatForever::create((CCActionInterval *)CCSequence::create(  
 CCTintTo::create(0.2f, 255, 0, 0),  
 CCTintTo::create(0.2f, 0, 255, 0),  
 CCTintTo::create(0.2f, 0, 0, 255),  
 CCTintTo::create(0.2f, 0, 255, 255),  
 CCTintTo::create(0.2f, 255, 255, 0),  
 CCTintTo::create(0.2f, 255, 0, 255),  
 CCTintTo::create(0.2f, 255, 255, 255),  
 NULL));  

 //讓拖尾執行這個變色動畫序列。  
m_streak->runAction(colorAction);

名就叫拖尾效果,所以這個效果如果不移動的話是看不見的,現在我們在update裡面移動它:

void HelloWorld::update(float delta)
{

bullt->setPosition(ccp(bullt->getPositionX(),bullt->getPositionY()+1));
m_streak->setPosition(bullt->getPosition());
}

ok,就這麼簡單

效果圖: