cocos2dx 2.x实现闪电效果(贴画版)

cocos2dx 2.x实现闪电效果(非画线版)

在网上搜索到一个直接用opengl画线实现的版本,但放在游戏中效果不太搭,要求用贴图的。我这个版本用的也是画线版的算法。

闪动的时候效果还可以,每段衔接的地方还是不太完美,各位有其他的建议的请联系我  blue-1986@hotmail.com

转载请注明出处 http://www.cnblogs.com/mrblue/p/4315091.html

效果图

cocos2dx 2.x实现闪电效果(贴画版)

cocos2dx 2.x实现闪电效果(贴画版)

//头文件

#ifndef __LIGINTNING_H__
#define __LIGINTNING_H__ #include "cocos2d.h"
#include <vector> class Lightning : public cocos2d::CCNode
{
public:
Lightning();
~Lightning(); public:
static Lightning* create(const std::string& strTextureFile, float fLength);
virtual bool init(const std::string& strTextureFile, float fLength); protected:
virtual void draw()/*override*/;
virtual void update(float fDelta)/*override*/; public:
void setTexture(cocos2d::CCTexture2D *texture);
void setLength(float fLength){ m_fLength = fLength; } protected:
void calculateVertex();
void addLightningSegment(float x1, float y1, float x2, float y2, float displace); protected:
std::vector<cocos2d::CCPoint> m_vecLightningTrackPoint;
std::vector<float> m_vecVertics;
std::vector<float> m_vecCoordinates;
cocos2d::CCTexture2D* m_pTexture;
float m_fLength;
}; #endif // __HELLOWORLD_SCENE_H__

  

//cpp

#include "Lightning.h"

USING_NS_CC;

using namespace std;

Lightning::Lightning()
:m_pTexture(NULL)
{
m_vecLightningTrackPoint.reserve();
m_vecVertics.reserve();
m_vecCoordinates.reserve();
} Lightning::~Lightning()
{
CC_SAFE_RELEASE(m_pTexture);
} Lightning* Lightning::create( const std::string& strTextureFile, float fLength )
{
Lightning * pRet = new Lightning();
if (pRet && pRet->init(strTextureFile,fLength))
{
pRet->autorelease();
}
else
{
CC_SAFE_DELETE(pRet);
} return pRet;
} bool Lightning::init(const string& strFile, float fLength)
{
CCNode::init(); setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTexture));
setLength(fLength);
setTexture(CCTextureCache::sharedTextureCache()->addImage(strFile.c_str())); scheduleUpdate(); return true;
} void Lightning::draw()
{
if(m_vecVertics.empty()||m_vecCoordinates.empty()) return; CC_NODE_DRAW_SETUP();
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords ); ccGLBlendFunc( CC_BLEND_SRC,CC_BLEND_DST );
ccGLBindTexture2D( m_pTexture->getName() ); glVertexAttribPointer(kCCVertexAttrib_Position, , GL_FLOAT, GL_FALSE, , (GLvoid*)(&m_vecVertics[]));
glVertexAttribPointer(kCCVertexAttrib_TexCoords, , GL_FLOAT, GL_FALSE, , (GLvoid*)(&m_vecCoordinates[])); glDrawArrays(GL_TRIANGLE_STRIP, , m_vecVertics.size()/);
} void Lightning::addLightningSegment( float x1, float y1, float x2, float y2, float displace )
{
if (displace <=m_pTexture->getPixelsWide()/ )
{
m_vecLightningTrackPoint.push_back(ccp(x2,y2));
}
else
{
float mid_x = (x2+x1)/;
float mid_y = (y2+y1)/;
mid_x += (CCRANDOM_0_1() - 0.5) * displace;
mid_y += (CCRANDOM_0_1() - 0.5) * displace;
addLightningSegment(x1, y1, mid_x, mid_y, displace/);
addLightningSegment(mid_x, mid_y,x2, y2, displace/);
}
} void Lightning::update(float delta)
{
calculateVertex();
} void Lightning::calculateVertex()
{
m_vecLightningTrackPoint.clear();
m_vecLightningTrackPoint.push_back(CCPointZero);
addLightningSegment(,,m_fLength,,m_fLength/); m_vecVertics.clear();
m_vecCoordinates.clear(); for (size_t i=; i<m_vecLightningTrackPoint.size(); i++)
{
const auto& pt = m_vecLightningTrackPoint[i]; m_vecVertics.push_back(pt.x);
m_vecVertics.push_back(pt.y-m_pTexture->getPixelsHigh()/);
m_vecVertics.push_back(pt.x);
m_vecVertics.push_back(pt.y+m_pTexture->getPixelsHigh()/); m_vecCoordinates.push_back(i);
m_vecCoordinates.push_back();
m_vecCoordinates.push_back(i);
m_vecCoordinates.push_back();
}
} void Lightning::setTexture( cocos2d::CCTexture2D *texture )
{
CC_SAFE_RETAIN(texture);
CC_SAFE_RELEASE(m_pTexture);
m_pTexture = texture;
  ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
m_pTexture->setTexParameters(&params);
}

代码下载

上一篇:算法:POJ1007 DNA sorting


下一篇:解决 com.alibaba.fastjson.JSONException: autoType is not support 异常处理