歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> coco2d-x 實現風雪飄動效果

coco2d-x 實現風雪飄動效果

日期:2017/3/1 10:41:44   编辑:Linux編程

項目中的天氣系統,需要用到風雪效果,這時模擬的風雪效果,在3d效果上,還有點不足,就是雪花的消失點應該按照雪花的縮放系數算出它該消失的位置。目前是用的在屏幕外面就從新移到原始位置。上個效果圖:

具體實現:

先定義幾個雪花飄的方向

//先定義幾個雪花的飄動方向

enum{

tag_batch_node=0,

tag_wind_none=1,

tag_wind_left=2,

tag_wind_right=3

};

//初始化雪花

voidGTWind::showWind(){

//初始的飄動方向

m_windDir=tag_wind_none;

//重力的值,負數向下

g=-4;

//每一幀構造的雪花書

maxV=2;

//當前屏幕最大雪花數

maxCount=150;

//風速

windVelocity=0;

winSize=CCDirector::sharedDirector()->getWinSize();

//使用SpriteBatchNode初始化,讓所有雪花共享一塊內存,

//而且只需要調用一次OPENGL繪畫,就可以構造所有雪花,提高效率

CCSpriteBatchNode *spriteBatchNode = CCSpriteBatchNode::batchNodeWithFile("snow.png",300);

addChild(spriteBatchNode,0,tag_batch_node);

schedule(schedule_selector(GTWind::changeWind),5.0);

schedule(schedule_selector(GTWind::updataWind));

}

上面使用的schedule來每一幀待用改變雪花位置,以及每5秒隨機一次風向


現在隨即風向,並調用函數動態改變當前的風速

//改變風的方向

voidGTWind::changeWind(){

int dir=arc4random()%3+1;

if(m_windDir==dir){

return;

}

m_windDir=dir;

switch (m_windDir) {

case tag_wind_none:

//規定雪花的最大速度

maxWindVelocity=0;

//動態改變速度

schedule(schedule_selector(GTWind::upDataWindVelocity));

break;

case tag_wind_left:

maxWindVelocity=20;

schedule(schedule_selector(GTWind::upDataWindVelocity));

break;

case tag_wind_right:

maxWindVelocity=-20;

schedule(schedule_selector(GTWind::upDataWindVelocity));

break;

default:

break;

}

}


這個我以前就寫過的,用於游戲中動態改變數值的

//動態改變當前的風速

voidGTWind::upDataWindVelocity(){

int addV=maxWindVelocity-windVelocity;

if(abs(addV)>10){

windVelocity+=addV/10;

}elseif(abs(addV)>2 &&abs(addV)<=10){

windVelocity+=addV/abs(addV);

}else{

windVelocity=maxWindVelocity;

unschedule(schedule_selector(GTWind::upDataWindVelocity));

}

}


//刷新所有的雪花

voidGTWind::updataWind(ccTime dt){

CCSpriteBatchNode *temSpriteBatchNode = (CCSpriteBatchNode *)getChildByTag(tag_batch_node);

if(temSpriteBatchNode->getChildren()->count()<maxCount){

for(int i=0;i<maxV;i++){

//從SpriteBatchNode讀取貼圖創建sprite,並加入到SpriteBatchNode

CCSprite* sprite=CCSprite::spriteWithTexture(temSpriteBatchNode->getTexture());

temSpriteBatchNode->addChild(sprite);

sprite->setScale(1.0f - (arc4random()%5+5) / 10.0);

//初始化每個雪花的位置

if (windVelocity >0)

sprite->setPosition(ccp(winSize.width+10,1.0*(arc4random()%((int)winSize.height+200))));

else

sprite->setPosition(ccp(-10,1.0*(arc4random()%((int)winSize.height+200))));

if (windVelocity <3 && windVelocity > -3)

sprite->setPosition(ccp(1.0*(arc4random()%((int)winSize.height+240)),winSize.height+200));

}

}

//得到所有雪花,改變位置

CCArray* allSprite=temSpriteBatchNode->getChildren();

CCObject* pObject = NULL;

CCARRAY_FOREACH(allSprite, pObject){

CCSprite* pChild = (CCSprite*) pObject;

CCPoint pNow=pChild->getPosition();

pNow.x-=pChild->getScale()*windVelocity;

pNow.y+=g;

pChild->setPosition(pNow);

pChild->setRotation(pChild->getRotation()+0.1f);

if(pChild->getPosition().x<-10 ||

pChild->getPosition().x>winSize.width+10 ||

pChild->getPosition().y<-10 ){

if (windVelocity >0)

pChild->setPosition(ccp(winSize.width+10,1.0*(arc4random()%((int)winSize.height+200))));

else

pChild->setPosition(ccp(-10,1.0*(arc4random()%((int)winSize.height+200))));

if (windVelocity <3 && windVelocity > -3)

pChild->setPosition(ccp(1.0*(arc4random()%((int)winSize.height+240)),winSize.height+10));

}

}

}

好了,基本效果就這樣了,可以改進的地方就是雪花的消失點,要根據每個雪花的縮放系數算出消失點,這樣才有3D的效果

Copyright © Linux教程網 All Rights Reserved