CocosCreator 源码cc.Blink详解

欢迎关注公众号:“Cocos Creator 源码讲解”,一起学习。

/* Blinks a Node object by modifying it's visible property
 * @class Blink
 * @extends ActionInterval
 * @param {Number} duration  duration in seconds
 * @param {Number} blinks  blinks in times
 * @example
 * var action = new cc.Blink(2, 10);
 */
/* 通过修改 Node 对象的可见属性来使其闪烁
 * @类眨眼
 * @extends ActionInterval
 * @param {Number} 持续时间 持续时间(以秒为单位)
 * @param {Number} 闪烁 闪烁次数
 * @例子
 * var action = new cc.Blink(2, 10);
 */
cc.Blink = cc.Class({
    name: 'cc.Blink',
    extends: cc.ActionInterval,

    ctor: function (duration, blinks) {
        this._times = 0;
        /* 原始状态 */
        this._originalState = false;
        blinks !== undefined && this.initWithDuration(duration, blinks);
    },

    /*
     * Initializes the action.
     * @param {Number} duration duration in seconds
     * @param {Number} blinks blinks in times
     * @return {Boolean}
     */
    /*
     * 初始化操作。
     * @param {Number} 持续时间 持续时间(以秒为单位)
     * @param {Number} 闪烁 闪烁次数
     * @return {布尔值}
     */
    initWithDuration: function (duration, blinks) {
        if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
            this._times = blinks;
            return true;
        }
        return false;
    },

    clone: function () {
        var action = new cc.Blink();
        this._cloneDecoration(action);
        action.initWithDuration(this._duration, this._times);
        return action;
    },

    update: function (dt) {
        dt = this._computeEaseTime(dt);
        if (this.target && !this.isDone()) {
            var slice = 1.0 / this._times;
            var m = dt % slice;
            this.target.opacity = (m > (slice / 2)) ? 255 : 0;
        }
    },
    /* 设置target   */
    startWithTarget: function (target) {
        cc.ActionInterval.prototype.startWithTarget.call(this, target);
        this._originalState = target.opacity;
    },
    /* 停止action */
    stop: function () {
        this.target.opacity = this._originalState;
        cc.ActionInterval.prototype.stop.call(this);
    },
    /* 逆向action */
    reverse: function () {
        var action = new cc.Blink(this._duration, this._times);
        this._cloneDecoration(action);
        this._reverseEaseList(action);
        return action;
    }
});
/**
 * !#en Blinks a Node object by modifying it's visible property.
 * !#zh 闪烁(基于透明度)。
 * @method blink
 * @param {Number} duration  duration in seconds
 * @param {Number} blinks blinks in times
 * @return {ActionInterval}
 * @example
 * // example
 * var action = cc.blink(2, 10);
 */
/* 创建blink对象 */
cc.blink = function (duration, blinks) {
    return new cc.Blink(duration, blinks);
};

cocos2dx 雷电MoonWarriors_游戏源码 #include "GameLayer.h" #include "SimpleAudioEngine.h" #include "Bullet.h" #include "Resource.h" #include "Config.h" #include "Enemy.h" #include "Effect.h" #include "GameOver.h" #include "PauseLayer.h" using namespace cocos2d; using namespace CocosDenshion; bool isPaused = false; GameLayer::GameLayer():m_state(statePlaying),m_time(0),m_ship(NULL),m_backSky(NULL),m_backSkyHeight(0),m_backSkyRe(NULL),m_backTileMap(NULL),m_backTileMapHeight(0),m_backTileMapRe(NULL),m_isBackSkyReload(false),m_isBackTileReload(false),m_lbScore(NULL),m_lifeCount(NULL), m_tempScore(0) { } GameLayer::~GameLayer() { if (m_levelManager) { delete m_levelManager; } play_bullet->release(); enemy_bullet->release(); enemy_items->release(); } bool GameLayer::init() { if (!CCLayer::init()) { return false; } // 开启触摸 this->setTouchEnabled(true); // 创建数组,需要retain一下 play_bullet = CCArray::create(); play_bullet->retain(); enemy_bullet = CCArray::create(); enemy_bullet->retain(); enemy_items = CCArray::create(); enemy_items->retain(); m_state = statePlaying; Enemy::sharedEnemy(); Effect::sharedExplosion(); Config::sharedConfig()->resetConfig(); winSize = CCDirector::sharedDirector()->getWinSize(); m_levelManager = new LevelManager(this); //初始化背景 initBackground(); m_screenRec = CCRectMake(0, 0, winSize.width, winSize.height + 10); // score m_lbScore = CCLabelBMFont::create("Score:0", s_arial14_fnt); m_lbScore->setAnchorPoint(ccp(1, 0)); m_lbScore->setAlignment(kCCTextAlignmentRight); addChild(m_lbScore, 1000); m_lbScore->setPosition(winSize.width - 5, winSize.height - 30); // ship life CCTexture2D *shipTexture = CCTextureCache::sharedTextureCache()->addImage(s_ship01); CCSprite *life = CCSprite::createWithTexture(shipTexture, CCRectMake(0, 0, 60, 38)); life->setScale(0.6); life->setPosition(ccp(30,winSize.height-23)); addChild(life, 1, 5); // ship life count char lifecount[2]; sprintf(lifecount, "%d",Config::sharedConfig()->getLifeCount()); m_lifeCount = CCLabelTTF::create(lifecount, "Arial", 20); m_lifeCount->setPosition(ccp(60, winSize.height-20)); m_lifeCount->setColor(ccc3(255,0, 0)); addChild(m_lifeCount, 1000); // ship m_ship = Ship::create(); addChild(m_ship, m_ship->getZoder(), 1001); CCMenuItemImage *pause = CCMenuItemImage::create("pause.png", "pause.png", this, menu_selector(GameLayer::doPause)); pause->setAnchorPoint(ccp(1, 0)); pause->setPosition(ccp(winSize.width, 0)); CCMenu *menu = CCMenu::create(pause, NULL); menu->setAnchorPoint(ccp(0, 0)); addChild(menu, 1, 10); menu->setPosition(CCPointZero); // 调 update函数 scheduleUpdate(); // 每秒调一次 scoreCounter函数 schedule(schedule_selector(GameLayer::scoreCounter), 1); if (Config::sharedConfig()->getAudioState()) { SimpleAudioEngine::sharedEngine()->playBackgroundMusic(s_bgMusic, true); } return true; } void GameLayer::update(float dt) { if (m_state == statePlaying) { checkIsCollide(); removeInactiveUnit(dt); checkIsReborn(); updateUI(); } } void GameLayer::scoreCounter() { if (m_state == statePlaying) { m_time++; m_levelManager->loadLevelResource(m_time); } } void GameLayer::checkIsCollide() { CCObject *units; CCObject *bullets; CCObject *enemybs; CCARRAY_FOREACH(enemy_items, units) { UnitSprite *enemy = dynamic_cast<UnitSprite*>(units); CCARRAY_FOREACH(play_bullet, bullets) { UnitSprite *bullet = dynamic_cast<UnitSprite*>(bullets); if (this->collide(enemy, bullet)) { enemy->hurt(); bullet->hurt(); } if (!(m_screenRec.intersectsRect(bullet->boundingBox()))) { bullet->destroy(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值