• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
linux_ios
博客园    首页    新随笔    联系   管理    订阅  订阅

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;
}

 

posted @ 2013-04-08 17:28  linux_ios  阅读(708)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3