【cocos2d-x IOS游戏开发-城市跑酷7】设计烟囱与烟的效果

尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17202009


<捕鱼达人>回顾

【cocos2d-x IOS游戏开发-捕鱼达人1】内容介绍

上节回顾

【cocos2d-x IOS游戏开发-城市跑酷6】添加街区元素:墙、屋顶、烟囱


有了烟囱就该有烟的效果:

设计烟的动画效果:

    CCAnimation* animation;
    animation = CCAnimation::create();
    CCSpriteFrame * frame;

    for(i = 1; i <= 4; i++) {
        char szName[100] = {0};
        sprintf(szName, "puff_%i.png", i);
        frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(szName);
        animation->addSpriteFrame(frame);
    }

    animation->setDelayPerUnit(0.75f / 4.0f);
    animation->setRestoreOriginalFrame(false);
    animation->setLoops(-1);
    _puffAnimation = CCAnimate::create(animation);
    _puffAnimation->retain();

    _puffSpawn = CCRepeat::create(CCSequence::create(CCDelayTime::create(0.5f),
                                  CCCallFunc::create( this, callfunc_selector(Block::createPuff) ),
                                  NULL), TOTAL_PUFFS);
    _puffSpawn->retain();

    _puffMove = CCMoveBy::create(1.0f, ccp(-100,80));
    _puffMove->retain();
    _puffFade = CCFadeOut::create(2.0f);
    _puffFade->retain();
    _puffScale = CCScaleBy::create(1.5f, 1.5);
    _puffScale->retain();

烟的创建:

void Block::createPuff () {
    
    int count = _chimneys->count();
    CCSprite * chimney;
    CCSprite * puff;
    
    for (int i = 0; i < count; i++) {
        chimney = (CCSprite * ) _chimneys->objectAtIndex(i);
        if (chimney->isVisible()) {
            
            puff = (CCSprite *) chimney->getChildByTag(_puffIndex);
            puff->setVisible(true);
            puff->stopAllActions();
            puff->setScale(1.0);
            puff->setOpacity(255);
            puff->setPosition(ccp(0,0));
            puff->runAction((CCAction *) _puffAnimation->copy()->autorelease());
            puff->runAction((CCAction *) _puffMove->copy()->autorelease());
            //puff->runAction((CCAction *) _puffFade->copy()->autorelease());
            puff->runAction((CCAction *) _puffScale->copy()->autorelease());
            
        }
    }
    
    _puffIndex++;
    if (_puffIndex == TOTAL_PUFFS) 
		_puffIndex = 0;
}

喷烟和隐藏烟:

//设置喷烟
void Block::setPuffing(bool value){
	_puffing = value;

	if (value) {
        this->runAction((CCAction *) _puffSpawn->copy()->autorelease());
		//
		CCAction * hide = CCSequence::create(CCDelayTime::create(2.5f),
			CCCallFunc::create( this, callfunc_selector(Block::hidePuffs) ),
			NULL);
        this->runAction(hide);
	} else {
		//reset all puffs
		_puffIndex = 0;
		int count = _chimneys->count();
		CCSprite * chimney;
		CCSprite * puff;

		for (int i = 0; i < count; i++) {
			chimney = (CCSprite * ) _chimneys->objectAtIndex(i);
			//每个烟囱冒出三朵烟
			for (int j = 0; j < TOTAL_PUFFS; j++) {
				puff = (CCSprite *) chimney->getChildByTag(j);
				puff->setVisible(false);
				puff->stopAllActions();
				//整体缩放,s为比例,s = 1表示原尺寸
				puff->setScale(1.0);
				//设置透明度,s范围0-255,0完全透明,255完全不透明
				puff->setOpacity(255);
				puff->setPosition(ccp(0,0));
			}
		}

	}
}

void Block::hidePuffs() {
	setPuffing(false);
}

下一节:实现街区房子的摆放

你可能感兴趣的:(动画,cocos2d-x,游戏开发)