// 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;
}
示例#2
0
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;
}