从Delphi开始学Cocos2dx-3.0[14]:动画-直接从精灵帧缓存中获取纹理

来源:互联网 发布:淘宝首页怎么添加视频 编辑:程序博客网 时间:2024/06/05 18:45

cocos2d 中有纹理和精灵帧两个概念,当精灵要加载整个png图片的时候,纹理和精灵帧其实是一样的内容. 多数的时候, 精灵帧只是纹理的其中一部分


TScene* THelloWorld::createScene(){    // 'scene' is an autorelease object    auto scene = TScene::create();        // 'layer' is an autorelease object    auto layer = THelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);// 获取纹理缓存指针auto cache = TTextureCache::getInstance();// 加载纹理cache->addImage("all.png");    // return the scene    return scene;}
把6个动作按顺序排好,并且放到一张png上 加载



bool THelloWorld::onTouchBegan(TTouch* touch, TEvent* event){ auto sprite =  (TSprite*)(this->getChildByTag(1000));            //sprite->stopAllActions();      sprite->cleanup();      sprite->runAction(TPlace::create(TPoint(100.0f, g_ClientMidY)));        auto animation = TAnimation::create();      // 转载图片      for (int i = 1; i <= 6; i++)      {  // 加载精灵帧auto frame = TSpriteFrame::create("all.png", TRect(100 * i - 100,0,100,128));// 改成添加精灵帧animation->addSpriteFrame(frame);    }        // 设置动画播放的属性 2秒 6帧      animation->setDelayPerUnit(1.2f / 6.0f);        // 设置精灵帧的使用方式, 做完动画还原成初始帧      animation->setRestoreOriginalFrame(false);        // 重复10次      animation->setLoops(3);        // 创建动画动作      auto action = TAnimate::create(animation);        sprite->runAction(TSpawn::create(action, TMoveBy::create(1.2f * 3,TPoint(500.0f, 0.0f)), NULL));        CCLOG("THelloWorld::onTouchBegan id = %d, x = %f, y = %f", touch->getID(), touch->getLocation().x, touch->getLocation().y);      return true;  }


0 0
原创粉丝点击