您的位置:首页 > 其它

coco2d-x 实现风雪飘动效果

2011-12-30 19:42 387 查看
猴子原创,欢迎转载,转载请注明出处。

原文地址: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的效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  random null 游戏