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