cocos2dx 自定义动画 手动添加

bool HelloWorld::init()

{

    //////////////////////////////

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

    

//    CCSprite * sp= CCSprite::create("crop1.png");

//    sp->setPosition(ccp(170, 200));

//    addChild(sp);

//    

//    CCAnimation * animation = CCAnimation::create();

//    animation->addSpriteFrameWithFileName("crop1.png");

//    animation->addSpriteFrameWithFileName("crop2.png");

//    animation->addSpriteFrameWithFileName("crop3.png");

//    animation->addSpriteFrameWithFileName("crop4.png");

//    

//    animation->setDelayPerUnit(2.8f/14.0f);//必须设置否则不会动态播放

//    animation->setRestoreOriginalFrame(true);//是否回到第一帧

//    animation->setLoops(-1);//重复次数(-1, 无限循环)

//    CCFiniteTimeAction * animate = CCAnimate::create(animation);

//    sp->runAction(animate);

    CCTexture2D::PVRImagesHavePremultipliedAlpha(true);

    CCTexture2D::PVRImagesHavePremultipliedAlpha(true);

    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("corp.plist");

//    利用帧缓存创建精灵

    CCSprite * sp =CCSprite::createWithSpriteFrameName("crop1.png");

    sp->setPosition(ccp(170, 200));

    addChild(sp);

    

    CCArray * animFrames = CCArray::createWithCapacity(4);

    char str[100]={0};

    for (int i = 1 ; i<5; i++) {

        sprintf(str, "crop%i.png",i);

        CCSpriteFrame *fream= CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(str);

        animFrames->addObject(fream);

    }

    CCAnimation * animation = CCAnimation::createWithSpriteFrames(animFrames,0.3f);

    animation->setLoops(-1);

    sp->runAction(CCAnimate::create(animation));

    

    

    CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFrameByName("corp.plist");

    

    

    return true;

}

 

你可能感兴趣的:(cocos2dx)