coco2d-x 实现风雪飘动效果

猴子原创,欢迎转载,转载请注明出处。

原文地址:http://blog.csdn.net/yanghuiliu/article/details/7166039

项目中的天气系统,需要用到风雪效果,这时模拟的风雪效果,在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的效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值