CCAnimate* CCAnimate::actionWithDuration(ccTime duration, CCAnimation *pAnimation, bool bRestoreOriginalFrame) { CCAnimate *pAnimate = new CCAnimate(); pAnimate->initWithDuration(duration, pAnimation, bRestoreOriginalFrame); pAnimate->autorelease(); return pAnimate; }
// // Animate // CCAnimate* CCAnimate::actionWithAnimation(CCAnimation *pAnimation) { CCAnimate *pAnimate = new CCAnimate(); pAnimate->initWithAnimation(pAnimation, true); pAnimate->autorelease(); return pAnimate; }
CCAnimate* CCAnimate::actionWithAnimation(CCAnimation *pAnimation, bool bRestoreOriginalFrame) { CCAnimate *pAnimate = new CCAnimate(); pAnimate->initWithAnimation(pAnimation, bRestoreOriginalFrame); pAnimate->autorelease(); return pAnimate; }
void YHCCActionHelper::runIntervalForeverAnimation(float interval, CCAnimation * animation, CCSprite * pSprite) { CCAnimate * animate = CCAnimate::create(animation); CCDelayTime * delay = CCDelayTime::create(interval); CCSequence * sequence = CCSequence::create(animate, delay, NULL); CCRepeatForever * forever = CCRepeatForever::create(sequence); CCAnimationFrame * animationFrame = static_cast<CCAnimationFrame *>(animate->getAnimation()->getFrames().at(0)); pSprite->setDisplayFrame(animationFrame->getSpriteFrame()); pSprite->runAction(forever); }
CCFiniteTimeAction* AppConfig::getActionWithAnimate(const char* animateName, float duration) { CCAnimation* pAnimation = CCAnimationCache::sharedAnimationCache()->animationByName(animateName); if(pAnimation){ CCAnimate* pAnimate = CCAnimate::create(pAnimation); pAnimate->setDuration(duration); pAnimation->setRestoreOriginalFrame(false); return pAnimate; } return NULL; }
bool Fish::init(FishType type) { _type = type; CCString* animationName = CCString::createWithFormat(STATIC_DATA_STRING("fish_animation"), _type); CCAnimation* fishAnimation = CCAnimationCache::sharedAnimationCache()->animationByName(animationName->getCString()); CCAnimate* fishAnimate = CCAnimate::create(fishAnimation); fishAnimate->setTag(k_Action_Animate); _fishSprite = CCSprite::create(); this->addChild(_fishSprite); _fishSprite->runAction(CCRepeatForever::create(fishAnimate)); return true; }
//----------------------------------------------------------------------------- bool CFish::init(EFishType type) { //CCAnimation只是用于存放动画,真正让动画播放起来的是动作类CCAnimate //CCAnimate只能由CCSprite及其子类播放 m_type = type; CCString* animationName = CCString::createWithFormat(STATIC_DATA_STRING("fish_animation"), m_type); CCAnimation* fishAnimation = CCAnimationCache::sharedAnimationCache()->animationByName(animationName->getCString()); CCAnimate* fishAnimate = CCAnimate::create(fishAnimation); fishAnimate->setTag(k_Action_Animate); m_fishSprite = CCSprite::create(); this->addChild(m_fishSprite); m_fishSprite->runAction(CCRepeatForever::create(fishAnimate)); return true; }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } CCSize size = CCDirector::sharedDirector()->getWinSize(); //缓存图片文件及配置 CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("L005Animation.plist", "L005Animation.png"); //创建数组 CCArray* frames = CCArray::create(); char name[10]; //根据名称获取SpriteFrame对象,并填入数组中 for (int i=0; i<30; i++) { //将int类型的数据转变成字符串 sprintf(name, "a%04d",i); //将SpriteFrame对象添加到数组中 frames->addObject(cache->spriteFrameByName(name)); } //根据生成的数组创建CCAnimation动画配置对象 CCAnimation* animation = CCAnimation::createWithSpriteFrames(frames,0.02); //根据动画配置对象创建CCAnimate对象 CCAnimate* animate = CCAnimate::create(animation); //创建CCSprite对象用于呈现动画 CCSprite* anim = CCSprite::create(); //调整位置 anim->setPosition(ccp(size.width/2, size.height/2)); //添加到层中 addChild(anim); //执行动画 // anim->runAction(CCRepeatForever::create(animate)); //反向执行动画 anim->runAction(CCRepeatForever::create(animate->reverse())); return true; }
void AvatarComponent::setAnimation(CCAnimation * animation) { this->stopActionByTag(s_animationTag); CC_SAFE_RELEASE_NULL(m_animation); if (animation != nullptr) { m_animation = animation; m_animation->retain(); this->setSpriteFrame(animation->getFrames().at(0)->getSpriteFrame()); CCAnimate * animate = CCAnimate::create(animation); animate->setTag(s_animationTag); this->runAction(animate); } }
Player* Player::spriteWithFile(const char *pszFileName, int columns, int rows) { // CCSpriteFrame *frame = cacher->spriteFrameByName( pszFileName); CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(pszFileName); CCArray* aFrames = CCArray::createWithCapacity(columns * rows); float textureWidth = texture->getPixelsWide(); float textureHeight = texture->getPixelsHigh(); float frameWidth = textureWidth / columns; float frameHeight = textureHeight / rows; for (int c = 0; c < columns; c++) { for (int r = 0; r < rows; r++) { CCSpriteFrame *frame0 = CCSpriteFrame::createWithTexture(texture, CCRectMake((c*frameWidth), (r*frameHeight), 100, 65)); aFrames->addObject(frame0); } } CCAnimation *animation = CCAnimation::createWithSpriteFrames(aFrames, 0.2f); CCAnimate *animate = CCAnimate::create(animation); CCActionInterval* seq = (CCActionInterval*)(CCSequence::create( animate, animate->copy()->autorelease(), CCFlipX::create(false), NULL) ); Player *pobSprite = new Player(); if (pobSprite && pobSprite->initWithSpriteFrame((CCSpriteFrame*) aFrames->objectAtIndex(0))) { pobSprite->scheduleUpdate(); pobSprite->autorelease(); pobSprite->setForwardMarch(false); pobSprite->setMightAsWellJump(false); pobSprite->setBackwardMarch(false); pobSprite->runAction(CCRepeatForever::create( seq ) ); // pobSprite->setAnimFrames( aFrames); // pobSprite->setCurrentFrame(0); return pobSprite; } CC_SAFE_DELETE(pobSprite); return NULL; }
int ShieldedObject::playShieldAnimation(int damage, int angle) { CCAnimate* animate; map<int,cocos2d::CCAnimation*>::iterator it = getShieldAnimations()->find(0); if (it == getShieldAnimations()->end()) { return 0; } else { animate = CCAnimate::create(it->second); } animate->setDuration(0.13f); //runAction(animate); node->setAnchorPoint(ccp(0.5, 0.5)); node->setRotation(angle); node->runAction(animate); return 1; }
CCObject* CCAnimate::copyWithZone(CCZone *pZone) { CCZone* pNewZone = NULL; CCAnimate* pCopy = NULL; if(pZone && pZone->m_pCopyObject) { //in case of being called at sub class pCopy = (CCAnimate*)(pZone->m_pCopyObject); } else { pCopy = new CCAnimate(); pZone = pNewZone = new CCZone(pCopy); } CCActionInterval::copyWithZone(pZone); pCopy->initWithDuration(m_fDuration, m_pAnimation, m_bRestoreOriginalFrame); CC_SAFE_DELETE(pNewZone); return pCopy; }
void AvatarComponent::resetActions() { m_animatorData->getInitData()->initializeNode(this); m_animatorData->getAnimData()->runActions(this, m_animatorDataTags); // 针对 offset time 的处理 CCDelayTime * delayTime = m_animatorData->getAnimData()->getDelayTimeAction(); if (delayTime != nullptr) { this->setVisible(false); // 首先延迟 offset time 时间,然后显示动画(一定从第一帧开始)。 Vector<FiniteTimeAction *> actions; actions.pushBack(delayTime); actions.pushBack(Show::create()); actions.pushBack(CallFunc::create([this]{ this->stopActionByTag(s_animationTag); this->setSpriteFrame(m_animation->getFrames().at(0)->getSpriteFrame()); CCAnimate * animate = CCAnimate::create(m_animation); animate->setTag(s_animationTag); this->runAction(animate); })); // 动画播放结束后,继续延迟,直到时间结束后,设置不可见。 actions.pushBack(DelayTime::create(m_animatorData->getAnimData()->getSumTime() - m_animatorData->getAnimData()->getOffsetTime())); actions.pushBack(Hide::create()); Sequence * seq = Sequence::create(actions); Action * action = nullptr; if (m_animatorData->getAnimData()->isForever()) action = RepeatForever::create(seq); else action = seq; action->setTag(206); this->runAction(action); } }
void Player::introAction() { currentAnimation->setDelayPerUnit(0.09f); currentAnimation->setRestoreOriginalFrame(true); currentAnimation->addSpriteFrameWithFileName("yes1.png"); currentAnimation->addSpriteFrameWithFileName("yes2.png"); currentAnimation->addSpriteFrameWithFileName("yes3.png"); currentAnimation->addSpriteFrameWithFileName("yes4.png"); currentAnimation->addSpriteFrameWithFileName("yes5.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("yes6.png"); currentAnimation->addSpriteFrameWithFileName("wait7.png"); currentAnimation->addSpriteFrameWithFileName("wait8.png"); currentAnimation->addSpriteFrameWithFileName("wait9.png"); currentAnimation->addSpriteFrameWithFileName("wait10.png"); currentAnimation->addSpriteFrameWithFileName("wait11.png"); currentAnimation->addSpriteFrameWithFileName("wait12.png"); currentAnimation->addSpriteFrameWithFileName("wait11.png"); currentAnimation->addSpriteFrameWithFileName("wait10.png"); currentAnimation->addSpriteFrameWithFileName("wait9.png"); currentAnimation->addSpriteFrameWithFileName("wait8.png"); currentAnimation->addSpriteFrameWithFileName("wait9.png"); currentAnimation->addSpriteFrameWithFileName("wait10.png"); currentAnimation->addSpriteFrameWithFileName("wait11.png"); currentAnimation->addSpriteFrameWithFileName("wait12.png"); currentAnimation->addSpriteFrameWithFileName("wait11.png"); currentAnimation->addSpriteFrameWithFileName("wait10.png"); currentAnimation->addSpriteFrameWithFileName("wait9.png"); currentAnimation->addSpriteFrameWithFileName("wait8.png"); currentAnimation->addSpriteFrameWithFileName("wait7.png"); currentAnimation->addSpriteFrameWithFileName("wait6.png"); currentAnimation->addSpriteFrameWithFileName("wait5.png"); currentAnimation->addSpriteFrameWithFileName("wait4.png"); currentAnimation->addSpriteFrameWithFileName("wait3.png"); currentAnimation->addSpriteFrameWithFileName("wait2.png"); currentAnimation->addSpriteFrameWithFileName("wait1.png"); currentAnimation->addSpriteFrameWithFileName("wait2.png"); currentAnimation->addSpriteFrameWithFileName("wait3.png"); currentAnimation->addSpriteFrameWithFileName("wait4.png"); currentAnimation->addSpriteFrameWithFileName("wait5.png"); currentAnimation->addSpriteFrameWithFileName("wait4.png"); currentAnimation->addSpriteFrameWithFileName("wait3.png"); currentAnimation->addSpriteFrameWithFileName("wait2.png"); currentAnimation->addSpriteFrameWithFileName("wait1.png"); currentAnimation->addSpriteFrameWithFileName("wait2.png"); currentAnimation->addSpriteFrameWithFileName("wait3.png"); currentAnimation->addSpriteFrameWithFileName("wait4.png"); currentAnimation->addSpriteFrameWithFileName("wait5.png"); currentAnimation->addSpriteFrameWithFileName("wait6.png"); currentAnimation->addSpriteFrameWithFileName("wait7.png"); currentAnimation->addSpriteFrameWithFileName("wait8.png"); currentAnimation->addSpriteFrameWithFileName("wait9.png"); currentAnimation->addSpriteFrameWithFileName("wait10.png"); currentAnimation->addSpriteFrameWithFileName("wait11.png"); currentAnimation->addSpriteFrameWithFileName("wait12.png"); currentAnimation->addSpriteFrameWithFileName("wait11.png"); currentAnimation->addSpriteFrameWithFileName("wait10.png"); currentAnimation->addSpriteFrameWithFileName("wait9.png"); currentAnimation->addSpriteFrameWithFileName("wait8.png"); currentAnimation->addSpriteFrameWithFileName("wait9.png"); currentAnimation->addSpriteFrameWithFileName("wait10.png"); currentAnimation->addSpriteFrameWithFileName("wait11.png"); currentAnimation->addSpriteFrameWithFileName("wait12.png"); currentAnimation->addSpriteFrameWithFileName("wait11.png"); currentAnimation->addSpriteFrameWithFileName("wait10.png"); currentAnimation->addSpriteFrameWithFileName("wait9.png"); currentAnimation->addSpriteFrameWithFileName("wait8.png"); currentAnimation->addSpriteFrameWithFileName("wait7.png"); currentAnimation->addSpriteFrameWithFileName("wait6.png"); currentAnimation->addSpriteFrameWithFileName("wait5.png"); currentAnimation->addSpriteFrameWithFileName("wait4.png"); currentAnimation->addSpriteFrameWithFileName("wait3.png"); currentAnimation->addSpriteFrameWithFileName("wait2.png"); currentAnimation->addSpriteFrameWithFileName("wait1.png"); currentAnimation->addSpriteFrameWithFileName("wait2.png"); currentAnimation->addSpriteFrameWithFileName("wait3.png"); currentAnimation->addSpriteFrameWithFileName("wait4.png"); currentAnimation->addSpriteFrameWithFileName("wait5.png"); currentAnimation->addSpriteFrameWithFileName("wait4.png"); currentAnimation->addSpriteFrameWithFileName("wait3.png"); currentAnimation->addSpriteFrameWithFileName("wait2.png"); CCAnimate* animate = CCAnimate::create(currentAnimation); //CCRepeatForever* repeat = CCRepeatForever::create(animate); animate->setTag(333); sprite->runAction(animate); }
bool CMainSelect::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } this->setTouchEnabled(true); // 创建背景 CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite* backgroud = CCSprite::create("mainsel/bg.png"); backgroud->setPosition( ccp(size.width/2, size.height/2) ); this->addChild(backgroud); // 按钮 CCMenuItemImage *pLack = CCMenuItemImage::create("mainsel/lackNor.png", "mainsel/lackSel.png", this, menu_selector(CMainSelect::lakeCallback) ); CCMenuItemImage *pForest = CCMenuItemImage::create("mainsel/forestNor.png", "mainsel/forestSel.png", this, menu_selector(CMainSelect::forestCallback) ); CCMenuItemImage *pDesert = CCMenuItemImage::create("mainsel/desertNor.png", "mainsel/desertSel.png", this, menu_selector(CMainSelect::desertCallback) ); CCMenuItemImage *pPrairie = CCMenuItemImage::create("mainsel/prairieNor.png", "mainsel/prairieSel.png", this, menu_selector(CMainSelect::prairieCallback) ); pLack->setPosition( ccp(171, 437) ); pForest->setPosition( ccp(539, 401) ); pDesert->setPosition( ccp(864, 411) ); pPrairie->setPosition( ccp(392, 208) ); CCMenu* pMenu = CCMenu::create(pLack, pForest, pDesert, pPrairie, NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu, 1); // 创建sprite sheet CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("mainsel/jingyu.plist"); CCSpriteBatchNode *spriteSheet = CCSpriteBatchNode::create("mainsel/jingyu.png"); this->addChild(spriteSheet); // 创建对象 CCSprite *sprite = CCSprite::createWithSpriteFrameName("jingyu01.png"); sprite->setPosition(ccp(920,220)); spriteSheet->addChild(sprite, 0); CCArray *arrShang = CCArray::create(); // 动画帧数组 for(int i=1; i<=8; ++i) { CCString *name = CCString::createWithFormat("jingyu%02d.png", i); arrShang->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name->getCString())); } CCArray *arrPeng = CCArray::create(); // 动画帧数组 for(int i=9; i<=11; ++i) { CCString *name = CCString::createWithFormat("jingyu%02d.png", i); arrPeng->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name->getCString())); } CCArray *arrPengRev = CCArray::createWithArray(arrPeng); arrPengRev->reverseObjects(); arrPeng->addObjectsFromArray(arrPengRev); CCFiniteTimeAction *delay = CCDelayTime::create(1); CCAnimate* anShang = CCAnimate::create(CCAnimation::createWithSpriteFrames(arrShang, 0.25)); CCAnimate* anPeng = CCAnimate::create(CCAnimation::createWithSpriteFrames(arrPeng, 0.15)); sprite->runAction(CCRepeatForever::create((CCActionInterval*)CCSequence::create(anShang, delay, anPeng,anPeng, anPeng, anPeng, anPeng, anShang->reverse(),delay,delay,delay,NULL))); return true; }
void Animal::runActionWithMotion(MOTION motion) { CCAnimation *animation = CCAnimation::create(); animation->setDelayPerUnit(0.1f); for( int iFrame = 0; iFrame < SPRITE_FRAME; iFrame++ ) { CCTexture2D *pTexture = animates[motion][iFrame]; CCSize sizeTexture = pTexture->getContentSize(); animation->addSpriteFrameWithTexture(pTexture, CCRectMake(0, 0, sizeTexture.width, sizeTexture.height)); } CCAnimate *animate = CCAnimate::create(animation); CCFiniteTimeAction *action; CCPoint direction; animate->setDuration(duration[motion]); switch(motion) { case WALK_LEFT: case RUN_LEFT: direction = ccp(-1.f, -1.f); break; case WALK_RIGHT: case RUN_RIGHT: direction = ccp( 1.f, -1.f); break; case WALK_BACK_LEFT: case RUN_BACK_LEFT: direction = ccp(-1.f, 1.f); break; case WALK_BACK_RIGHT: case RUN_BACK_RIGHT: direction = ccp( 1.f, 1.f); break; } switch(motion) { case WALK_LEFT: case WALK_RIGHT: case WALK_BACK_LEFT: case WALK_BACK_RIGHT: { CCPoint walk = ccp(direction.x*walk_scale.x, direction.y*walk_scale.y); action = CCSequence::actions( CCSpawn::actions(CCMoveBy::create(duration[motion], walk), animate, NULL), CCCallFuncN::actionWithTarget(this, callfuncN_selector(Animal::finishChk)), NULL); } break; case RUN_LEFT: case RUN_RIGHT: case RUN_BACK_LEFT: case RUN_BACK_RIGHT: { CCPoint run = ccp(direction.x*run_scale.x, direction.y*run_scale.y); action = CCSequence::actions( CCSpawn::actions(CCMoveBy::create(duration[motion], run), animate, NULL), CCCallFuncN::actionWithTarget(this, callfuncN_selector(Animal::finishChk)), NULL); } break; case STAND: case SIT: case SLEEP: case EAT: case POOP: case SICK: case FUN_SWING: case FUN_RUNNING: case FUN_ROPE: action = CCSequence::actions( animate, CCCallFuncN::actionWithTarget(this, callfuncN_selector(Animal::finishChk)), NULL); break; } motionState.name = motion; pBody->runAction(action); }