参考闪电特效算法:http://krazydad.com/bestiary/bestiary_lightning.html
算法详细信息可参上述网址
下面直接上代码,这其实就是一个算法问题:
.h文件
#ifndef __HELLO_liGHTING_H__#define __HELLO_liGHTING_H__#include "cocos2d.h"USING_NS_CC;class Hellolighting : public cocos2d::Layer{public: Hellolighting(voID); ~Hellolighting(voID); virtual bool init(); virtual voID update(float delta); static cocos2d::Scene* createScene(); CREATE_FUNC(Hellolighting); virtual voID draw(cocos2d::Renderer *renderer,const cocos2d::Mat4& transform,uint32_t flags); voID drawlighting(float x1,float y1,float x2,float y2,float displace);private: float curDetail; Point pos1; Point pos2;};#endif //__HELLO_liGHTING_H__
.cpp文件
#include "Hellolighting.h"Hellolighting::Hellolighting(voID){ }Hellolighting::~Hellolighting(voID){}cocos2d::Scene* Hellolighting::createScene(){ Scene *scene = Scene::create(); Hellolighting *layer = Hellolighting::create(); scene->addChild(layer); return scene;}bool Hellolighting::init(){ if ( !cclayer::init() ) { return false; } auto winSize = Director::sharedDirector()->getWinSize(); pos1 = Point(100,winSize.height/2); pos2 = Point(winSize.wIDth-100,winSize.height/2); curDetail = 5; return true;}voID Hellolighting::update( float delta ){}voID Hellolighting::draw(cocos2d::Renderer *renderer,uint32_t flags){ ccDrawcolor4B(255,0); gllinewidth(1); drawlighting(pos1.x,pos1.y,pos2.x,pos2.y,200); //这里多画几条线就可以看到更多了}voID Hellolighting::drawlighting( float x1,float displace ){ if (displace < curDetail) { ccDrawline(ccp(x1,y1),ccp(x2,y2)); } else { float mID_x = (x2+x1)/2; float mID_y = (y2+y1)/2; mID_x += (CCRANDOM_0_1() - 0.5) * displace; mID_y += (CCRANDOM_0_1() - 0.5) * displace; drawlighting(x1,y1,mID_x,mID_y,displace/2); drawlighting(x2,y2,displace/2); }}
直接加载这个场景就可以看到效果了,其实效果图跟运行效果还是有差别的,先来欣赏一下吧!
总结以上是内存溢出为你收集整理的cocos2dx-3.2 之 闪电效果全部内容,希望文章能够帮你解决cocos2dx-3.2 之 闪电效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)