std::map<int, cocos2d::CCAnimation*>* ShieldedObject::getShieldAnimations() { if (shieldAnimations == NULL) { shieldAnimations = new map<int, cocos2d::CCAnimation*>; for (int i = 0; i < 1; i++) { CCAnimation *animation; animation = CCAnimation::create(); animation->retain(); animation->setDelayPerUnit(0.05f); animation->addSpriteFrameWithFileName("res/image/shield7.png"); animation->addSpriteFrameWithFileName("res/image/shield6.png"); animation->addSpriteFrameWithFileName("res/image/shield5.png"); animation->addSpriteFrameWithFileName("res/image/shield4.png"); animation->addSpriteFrameWithFileName("res/image/shield3.png"); animation->addSpriteFrameWithFileName("res/image/shield2.png"); animation->addSpriteFrameWithFileName("res/image/shield1.png"); animation->addSpriteFrameWithFileName("res/image/shield.png"); animation->addSpriteFrameWithFileName("res/image/shield.png"); animation->addSpriteFrameWithFileName("res/image/shield.png"); animation->addSpriteFrameWithFileName("res/image/shield1.png"); animation->addSpriteFrameWithFileName("res/image/shield2.png"); animation->addSpriteFrameWithFileName("res/image/shield3.png"); animation->addSpriteFrameWithFileName("res/image/shield4.png"); animation->addSpriteFrameWithFileName("res/image/shield5.png"); animation->addSpriteFrameWithFileName("res/image/shield6.png"); animation->addSpriteFrameWithFileName("res/image/shield7.png"); animation->addSpriteFrameWithFileName("res/image/shield0.png"); (*shieldAnimations)[i] = animation; } } return shieldAnimations; }
//------------------------------------------------------------------------- // 加载资源 void FKAnimateExRes::_LoadRes( const char* szResName ) { string strPathName = CCFileUtils::sharedFileUtils()->fullPathForFilename( szResName ); // 加载Plist vector<string> vecPlists = m_SaxDelegator.m_vecPlists; for( unsigned int i = 0; i < vecPlists.size(); ++i ) { string strPlistPath = CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile( vecPlists[i].c_str(), strPathName.c_str() ); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile( strPlistPath.c_str() ); } // 加载Animate vector<SAnimate> vecAnimates = m_SaxDelegator.m_vecAnimates; CCArray* pSpriteFramesArray = CCArray::create(); for( unsigned int i = 0; i < vecAnimates.size(); ++i ) { SAnimate tagAnimate = vecAnimates[i]; vector<string> vecSpriteFrames = tagAnimate.m_vecSpriteFrame; for( unsigned int j = 0; j < vecSpriteFrames.size(); ++j ) { CCSpriteFrame* pSpriteFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(vecSpriteFrames[j].c_str()); if( pSpriteFrame != NULL ) { pSpriteFramesArray->addObject( pSpriteFrame ); } } CCAnimation* pAnimation = CCAnimation::createWithSpriteFrames( pSpriteFramesArray, tagAnimate.m_fDelay ); SAnimationInfo tagInfo; tagInfo.m_pAnimation = pAnimation; tagInfo.m_bIsFlipX = tagAnimate.m_bIsFlipX; tagInfo.m_bIsFlipY = tagAnimate.m_bIsFlipY; m_mapAnimation[tagAnimate.m_strName] = tagInfo; pAnimation->retain(); pSpriteFramesArray->removeAllObjects(); m_setAnimationName.insert( tagAnimate.m_strName ); } // 保存Animate for( unsigned int i = 0; i < vecAnimates.size(); ++i ) { SAnimate tagAnimate = vecAnimates[i]; m_mapAnimateNameToAnimate[tagAnimate.m_strName] = tagAnimate; } // 保存Plist m_mapXMLPathToPlists[szResName] = vecPlists; // 加载回调函数 _LoadCallback( szResName ); m_bIsVaild = true; }
void MAnimation::loadFile(const std::string& fileName) { tinyxml2::XMLDocument doc; //std::string s = CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName.c_str()); //doc.LoadFile(s.c_str()); doc.LoadFile(fileName.c_str()); tinyxml2::XMLElement* root = doc.RootElement(); if(std::strcmp(version.c_str(), root->FirstAttribute()->Value()) != 0) { CCAssert(0, "版本不匹配"); } tinyxml2::XMLElement* animation; animation = root->FirstChildElement("animation"); while(animation != NULL) { tinyxml2::XMLElement* ele = animation->FirstChildElement("animationName"); std::string animationName = ele->GetText(); //属性 MAnimationProperty* property = new MAnimationProperty; float scale; float rotate; CCPoint position = CCPointZero; std::string strPosition = animation->FirstAttribute()->Value(); //position std::string::size_type pos = strPosition.find(","); std::string strX, strY; strX = strPosition.substr(0, pos); strY = strPosition.substr(pos + 1, strPosition.length()); float x = std::atof(strX.c_str()), y = std::atof(strY.c_str()); position.setPoint(x, y); animation->QueryFloatAttribute("scale", &scale); animation->QueryFloatAttribute("rotation", &rotate); property->positionX = x; property->positionY = y; property->scaleX = property->scaleY = scale; property->rotation = rotate; nameToAnimationProperty->insert(std::make_pair(animationName, property)); int frameCount; ele = animation->FirstChildElement("frameCount"); ele->QueryIntText(&frameCount); float delayPerUnit; ele = animation->FirstChildElement("delayPerUnit"); ele->QueryFloatText(&delayPerUnit); bool restoreOriginFrame; ele = animation->FirstChildElement("restoreOriginFrame"); ele->QueryBoolText(&restoreOriginFrame); int loops; ele = animation->FirstChildElement("loops"); ele->QueryIntText(&loops); //创建CCAnimation CCArray* arr = CCArray::create(); CCAnimation* ccanimation; ccanimation = CCAnimationCache::sharedAnimationCache()->animationByName(animationName.c_str()); if(ccanimation == NULL) { std::string plistName = animationName; plistName += ".plist"; CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile(plistName.c_str()); char c[100]; for(int i = 0; i < frameCount; ++i) { sprintf(c, "%s_%02d.png", animationName.c_str(), i); CCSpriteFrame* frame = cache->spriteFrameByName(c); arr->addObject(frame); } ccanimation = CCAnimation::createWithSpriteFrames(arr); ccanimation->setDelayPerUnit(delayPerUnit); ccanimation->setRestoreOriginalFrame(restoreOriginFrame); ccanimation->setLoops(loops); CCAnimationCache::sharedAnimationCache()->addAnimation(ccanimation, animationName.c_str()); //加入缓存 } ccanimation->retain(); nameToCCAnimation->insert(std::make_pair(animationName, ccanimation)); animation = animation->NextSiblingElement("animation"); } }
bool Level12::ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event) { CCPoint touchLocation = touch->getLocationInView(); touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation); if (spSun->boundingBox().containsPoint(touchLocation)) { spSun->stopAllActions(); SimpleAudioEngine::sharedEngine()->stopAllEffects(); SimpleAudioEngine::sharedEngine()->playEffect("sun.wav"); spSun->runAction(CCRotateBy::create(1.0f, 360.f)); return false; } if (spBall->boundingBox().containsPoint(touchLocation)) { spBall->stopAllActions(); spBall->setPosition(ccp(getXForLession1(150), getYForLession1(393))); SimpleAudioEngine::sharedEngine()->stopAllEffects(); SimpleAudioEngine::sharedEngine()->playEffect("ball.wav"); spBallShadow->setVisible(false); spBall->runAction(CCSequence::create(CCJumpBy::create(0.6f, ccp(0, 0), 150*G_SCALEY, 1),CCJumpBy::create(0.4f, ccp(0, 0), 80*G_SCALEY, 1),CCJumpBy::create(0.2f, ccp(0, 0), 40*G_SCALEY, 1), CCCallFunc::create(this, callfunc_selector(Level12::showShadow)),NULL)); return false; } if (spRock->boundingBox().containsPoint(touchLocation)) { spRock->stopAllActions(); spRock->setPosition(ccp(getXForLession1(410), G_SHEIGHT/2)); SimpleAudioEngine::sharedEngine()->stopAllEffects(); SimpleAudioEngine::sharedEngine()->playEffect("rock.wav"); spRockShadow->setVisible(false); spRock->runAction(CCSequence::create(CCJumpBy::create(1.f, ccp(0, 0), 150*G_SCALEY, 1),CCCallFunc::create(this, callfunc_selector(Level12::showShadow)), NULL)); return false; } if (spGrass->boundingBox().containsPoint(touchLocation)) { spGrass->stopAllActions(); SimpleAudioEngine::sharedEngine()->stopAllEffects(); SimpleAudioEngine::sharedEngine()->playEffect("grass.wav"); CCAnimation *animation = CCAnimation::create(); animation->setDelayPerUnit(0.5f); for (int i = 0; i < 2; i ++) { CCTexture2D *txtr = CCTextureCache::sharedTextureCache()->addImage(CCString::createWithFormat("grass_%d.png", i+1)->getCString()); CCSize size = txtr->getContentSize(); CCRect rect = CCRectMake(0, 0, size.width, size.height); CCSpriteFrame *frame = CCSpriteFrame::createWithTexture(txtr, rect); animation->addSpriteFrame(frame); } animation->retain(); spGrass->runAction(CCRepeat::create(CCAnimate::create(animation), 2)); return false; } if (spHitBox->boundingBox().containsPoint(touchLocation)) { SimpleAudioEngine::sharedEngine()->stopAllEffects(); SimpleAudioEngine::sharedEngine()->playEffect("water.wav"); } return false; }