从Delphi开始学Cocos2dx-3.0[14]:动画-直接从精灵帧缓存中获取纹理
2014-01-17 15:46
357 查看
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;
}
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;
}
相关文章推荐
- 从Delphi开始学Cocos2dx-3.0[13]:动画-直接从图片文件创建动画
- 从Delphi开始学Cocos2dx-3.0[15]:关于精灵帧和精灵帧缓存
- 从Delphi开始学Cocos2dx-3.0[12]:可变速度
- 从Delphi开始学Cocos2dx-3.0[0]: VS2012 与 安装 cocos2d-x-3.0alpha0
- 从Delphi开始学Cocos2dx-3.3[17]:从3.0 进化到3.3(详细图文)
- 从Delphi开始学Cocos2dx-3.0[10]:持续动作之旋转持续动作
- 从Delphi开始学Cocos2dx-3.0[9]:持续动作之缩放持续动作
- 从Delphi开始学Cocos2dx-3.0[5]:鼠标按下移动弹起与单点触摸
- 从Delphi开始学Cocos2dx-3.0[1]: AppDelegate 与 Application
- 从Delphi开始学Cocos2dx-3.0[7]:瞬发动作
- 从Delphi开始学Cocos2dx-3.0[16]:LUA脚本接入
- 从Delphi开始学Cocos2dx-3.0[4]:定义一个Delphi头,方便今后编程
- 从Delphi开始学Cocos2dx-3.0[2]:TObject 与 CCObject 看继承关系
- 从Delphi开始学Cocos2dx-3.0[3]:CCNode 可视组件的头
- 从Delphi开始学Cocos2dx-3.0[6]:拖拽一个精灵
- 从Delphi开始学Cocos2dx-3.0[11]:持续动作之组合动作
- 从Delphi开始学Cocos2dx-3.0[8]:持续动作之位移持续动作
- 当request某个属性值变化之后,它原本所付给的int 变量的值似乎没有跟着改变,必须int变量参与运算,如+1或-1,或者直接用request获取值才能得到想要的结果。否则只能得到最开始所付的值
- cocos2dx3.0-获取图片的某个像素值
- phaser 如何跳过开始场景,直接进入动画 (5)