cocos2d-x游戏技能CD的实现


用一张资源图像完成技能CD的动画实现,这里用到了其他博主分享的一个取灰色图片的代码,在此谢过!

BYGraySprite.h文件

#include "cocoa/CCGeometry.h"
#include "cocos2d.h"
USING_NS_CC;


class BYGraySprite : public CCSprite{
    
    
public:
    BYGraySprite();
    virtual ~BYGraySprite();
    static BYGraySprite* create(const char* pszFileName);
    bool initWithTexture(CCTexture2D* pTexture, const CCRect& tRect);
    virtual void draw();
    
    
};


#endif

BYGraySprite.cpp文件

#include "BYGraySprite.h"

BYGraySprite::BYGraySprite(){
    
    
}


BYGraySprite::~BYGraySprite(){
    
    
}


BYGraySprite* BYGraySprite::create( const char* pszFileName ){
    BYGraySprite* graySprite = new BYGraySprite;
    if (graySprite && graySprite->initWithFile(pszFileName)){
        graySprite->autorelease();
        return graySprite;
    }else{
        CC_SAFE_RELEASE(graySprite);
        return NULL;
    }
}


bool BYGraySprite::initWithTexture(CCTexture2D* pTexture, const CCRect& tRect ){
    do{
        CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, tRect));
        
        
        GLchar* pszFragSource =
        "#ifdef GL_ES \n \
        precision mediump float; \n \
        #endif \n \
        uniform sampler2D u_texture; \n \
        varying vec2 v_texCoord; \n \
        varying vec4 v_fragmentColor; \n \
        void main(void) \n \
        { \n \
        // Convert to greyscale using NTSC weightings \n \
        vec4 col = texture2D(u_texture, v_texCoord); \n \
        float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n \
        gl_FragColor = vec4(grey, grey, grey, col.a); \n \
        }";
        
        
        CCGLProgram* pProgram = new CCGLProgram();
        pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);
        this->setShaderProgram(pProgram);
        pProgram->release();
        CHECK_GL_ERROR_DEBUG();
        
        
        this->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
        this->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
        this->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
        CHECK_GL_ERROR_DEBUG();
        
        
        this->getShaderProgram()->link();
        CHECK_GL_ERROR_DEBUG();
        
        
        this->getShaderProgram()->updateUniforms();
        CHECK_GL_ERROR_DEBUG();
        
        
        return true;
    } while (0);
    return false;
}


void BYGraySprite::draw(){
    ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
    ccGLBlendFunc( m_sBlendFunc.src, m_sBlendFunc.dst );
    
    
    this->getShaderProgram()->use();
    this->getShaderProgram()->setUniformForModelViewProjectionMatrix();
    
    
    ccGLBindTexture2D( this->getTexture()->getName() );
    
    
#define kQuadSize sizeof(m_sQuad.bl)
    long offset = (long)&m_sQuad;
    
    
    // vertex
    int diff = offsetof( ccV3F_C4B_T2F, vertices);
    glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
    
    
    // texCoods
    diff = offsetof( ccV3F_C4B_T2F, texCoords);
    glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));  
    
    
    // color  
    diff = offsetof( ccV3F_C4B_T2F, colors);  
    glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));  
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);  
    CC_INCREMENT_GL_DRAWS(1);  
}




源码实现:

//灰白图的生成
    BYGraySprite* graySprite = BYGraySprite::create("Icon-72.png");
    graySprite->setPosition(ccp(240, 160));
    std::cout<<graySprite->getContentSize().height<<graySprite->getContentSize().width<<std::endl;
    graySprite->setAnchorPoint(ccp(.5, .5));
    this->addChild(graySprite,-1);
  CCSprite* normalSprite = CCSprite::create("Icon-72.png");
   CCProgressTo *action1 = CCProgressTo::create(2, 100);
    CCProgressTimer *cdAct = CCProgressTimer::create(normalSprite);
    cdAct->setType(kCCProgressTimerTypeRadial);
    cdAct->setPosition(ccp(240,160));
    cdAct->runAction(CCRepeatForever::create(action1));
    this->addChild(cdAct);

好了,笔记就做到这,仅用一张资源图就可以搞定的技能CD,mark一下,分享快乐~~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值