CCAnimation创建动画-沈大海cocos2d-x教程22

时间:2023-02-07 18:06:39

图片资源:

CCAnimation创建动画-沈大海cocos2d-x教程22    


CCSize s = CCDirector::sharedDirector()->getWinSize();

    //载入动画所需纹理图片
    CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("animations/dragon_animation.png");
    
    // manually add frames to the frame cache// 切图获取每一帧
    CCSpriteFrame *frame0 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*0, 132*0, 132, 132));
    CCSpriteFrame *frame1 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*1, 132*0, 132, 132));
    CCSpriteFrame *frame2 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*2, 132*0, 132, 132));
    CCSpriteFrame *frame3 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*3, 132*0, 132, 132));
    CCSpriteFrame *frame4 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*0, 132*1, 132, 132));
    CCSpriteFrame *frame5 = CCSpriteFrame::createWithTexture(texture, CCRectMake(132*1, 132*1, 132, 132));
    
    
    //
    // Animation using Sprite BatchNode
    //
    CCSprite* sprite = CCSprite::createWithSpriteFrame(frame0);
    sprite->setPosition( ccp( s.width/2-80, s.height/2) );
    addChild(sprite);
            
    CCArray* animFrames = CCArray::createWithCapacity(6);
    animFrames->addObject(frame0);
    animFrames->addObject(frame1);
    animFrames->addObject(frame2);
    animFrames->addObject(frame3);
    animFrames->addObject(frame4);
    animFrames->addObject(frame5);
            
    CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f);//这里定义了每一帧,和动画的间隔时间
    CCAnimate *animate = CCAnimate::create(animation);//这里创建了动画的Action
    CCActionInterval* seq = (CCActionInterval*)(CCSequence::create( animate,
                       CCFlipX::create(true),
                       animate->copy()->autorelease(),
                       CCFlipX::create(false),
                       NULL) );
    

    sprite->runAction(CCRepeatForever::create( seq ) );

/.//////////////////////////////////////////////////////////////////////////////////////////////////////////貌似很简单,可以在应用中用该方法加入各种特效

但真正游戏中的角色,NPC,需要做碰撞的内容最好还是用SpriteBatchNode 来管理每个Sprite 通过Schedule自定义编程的方式来实现运动和变化。