cocos2d-x颜色混合模式完成光照效果

前端之家收集整理的这篇文章主要介绍了cocos2d-x颜色混合模式完成光照效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用Cocosd-x3.2的颜色混合功能和裁剪功能完成光照效果,简单易用,效果图如下:

代码

  1. //底图,光照图(一般是有透明度的白色图) 光移动的时间,循环次数
  2. Node * HelloWorld::createFlashNode(const std::string &spName,const std::string &splashName,float duration,int loops)
  3. {
  4. auto clNode = ClippingNode::create();
  5. auto stecil = Sprite::create(spName);
  6. clNode -> setStencil(stecil);
  7. clNode -> setAlphaThreshold(0.1);
  8.  
  9. auto star = Sprite::create(spName);
  10. clNode -> addChild(star);
  11.  
  12. auto splash = Sprite::create(splashName);
  13. star -> addChild(splash);
  14. //关键代码
  15. splash -> setBlendFunc({GL_DST_COLOR,GL_ONE});
  16. // splash -> setPosition(splash->getContentSize().width/2-star->getContentSize().width,splash->getContentSize().height/2);
  17.  
  18. auto place = Place::create(Vec2(splash->getContentSize().width/2-star->getContentSize().width,splash->getContentSize().height/2));
  19. auto moto = MoveTo::create(duration,Vec2(splash->getContentSize().width/2+star->getContentSize().width,splash->getContentSize().height/2));
  20. auto seq = Sequence::create(place,moto,NULL);
  21. ActionInterval *repeat = nullptr;
  22. if (loops!=-1)
  23. {
  24. repeat = Repeat::create(seq,loops);
  25. }
  26. else
  27. {
  28. repeat = RepeatForever::create(seq);
  29. }
  30. splash -> runAction(repeat);
  31.  
  32. return clNode;
  33. }
  34.  
  35. //调用
  36. auto no = createFlashNode("xingxing.png","light.png",2);
  37. this -> addChild(no);
  38. no -> setPosition(300,300);

猜你在找的Cocos2d-x相关文章