cocos2d Sprite混合达到水流动效果
Posted 振长策而御宇内
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cocos2d Sprite混合达到水流动效果相关的知识,希望对你有一定的参考价值。
使用Sprite混合setBlendFunc方式达到流动效果
void MainHomeLayer::updateWaterFall(float dt)
static float offset = 0;
if(NULL != _waterFall)
_waterFall->removeFromParentAndCleanup(true);
CCSprite* background = (CCSprite*)this->getChildByTag(eMainHomeLayer_Background);
CCSprite * waterFall = CCSprite::create("img/mainHome/waterfall.png");
CCTexture2DPixelFormat pixelFormat = waterFall->getTexture()->getPixelFormat();
if(kTexture2DPixelFormat_RGBA8888 == pixelFormat ||
kTexture2DPixelFormat_RGB5A1 == pixelFormat ||
kTexture2DPixelFormat_RGBA4444 == pixelFormat)
float scaleX = background->getScaleX();
float scaleY = background->getScaleY();
///Render Texture Begin
CCSize textrueSize = CCSizeMake(waterFall->getContentSize().width * scaleX,waterFall->getContentSize().height * scaleY);
CCRenderTexture* renderTexture = CCRenderTexture::create(textrueSize.width, textrueSize.height, kTexture2DPixelFormat_RGBA8888);
renderTexture->begin();
ccBlendFunc blendFunc1 = GL_ONE,GL_ONE_MINUS_SRC_ALPHA;
waterFall->setBlendFunc(blendFunc1);
waterFall->setOpacity(30);
waterFall->setPosition(CCPointMake(textrueSize.width * 0.5, textrueSize.height * 0.5));
waterFall->setScaleX(scaleX);
waterFall->setScaleY(scaleY);
waterFall->visit();
ccBlendFunc blendFunc = GL_DST_ALPHA,GL_ONE_MINUS_DST_ALPHA;
ccTexParams tp = GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT;
float PIXRIS_PRR_SECOND_WATER = (CCRANDOM_0_1() * 20 + 20) * scaleY;
CCSprite * water = CCSprite::create("img/mainHome/Water02.jpg");
water->setBlendFunc(blendFunc);
water->setScaleX(scaleX);
water->setScaleY(scaleY);
water->getTexture()->setTexParameters(&tp);
water->setTextureRect(CCRectMake(0, -offset, water->getContentSize().width, water->getContentSize().height));
water->setPosition(CCPointMake(textrueSize.width * 0.5, textrueSize.height * 0.5));
water->visit();
renderTexture->end();
///Render Texture End
this->setWaterFall(renderTexture->getSprite());
this->getWaterFall()->removeFromParentAndCleanup(true);
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
this->getWaterFall()->setPosition(CCPointMake(winSize.width * 0.87, winSize.height * 0.8));
this->addChild(this->getWaterFall(),100);
offset += PIXRIS_PRR_SECOND_WATER * dt;
以上是关于cocos2d Sprite混合达到水流动效果的主要内容,如果未能解决你的问题,请参考以下文章