PhotoShop下载:https://www.adobe.com/cn/downloads.html
TexturePacker 下载:https://www.codeandweb.com/texturepacker
上网搞一张GIF动图
打开PhotoShop,右上角下拉菜单选择动感,换成动画编辑模式,每帧处理成背景透明
然后 菜单->文件->导出->将图层导出到文件
运行导出
进入导出的文件夹,删除文件名里的图层后缀,保持图片序列可读性
打开TexturePacker 免费版,
专业版需要自己去申请key https://www.codeandweb.com/request-free-license
点击 add smart folder,选择图片序列的文件夹
属性选择
Data Format 选择 cocos2d-x
Data file 为导出 .plist 路径, 存放在项目Resources目录下
header file 和 source file, 存放在项目Classes目录下
设置好后按 Publish sprite sheet 导出
项目文件夹Classes和Resources下文件已添加
*如果是免费版会有几帧会被强x掉
切回VisualStudio
在解决方案 src文件夹中添加现有项,把 knight.cpp 和 knight.hpp 添加进来
打开 knight.hpp 可以看到,贴图基本功能都已经写好了
#ifndef __KNIGHT_H__
#define __KNIGHT_H__
#include "cocos2d.h"
namespace TexturePacker
{
class Knight
{
public:
static void addSpriteFramesToCache();
/**
* Remove sprite frames contained in theKnight sheet from
* the SpriteFrameCache.
*/
static void removeSpriteFramesFromCache();
// ---------------------
// sprite name constants
// ---------------------
static const std::string cha_kni_f_0000;
static const std::string cha_kni_f_0001;
static const std::string cha_kni_f_0002;
static const std::string cha_kni_f_0003;
static const std::string cha_kni_f_0004;
static const std::string cha_kni_f_0005;
// --------------
// Sprite objects
// --------------
static cocos2d::Sprite *createCha_kni_f_0000Sprite();
static cocos2d::Sprite *createCha_kni_f_0001Sprite();
static cocos2d::Sprite *createCha_kni_f_0002Sprite();
static cocos2d::Sprite *createCha_kni_f_0003Sprite();
static cocos2d::Sprite *createCha_kni_f_0004Sprite();
static cocos2d::Sprite *createCha_kni_f_0005Sprite();
// ----------------
// animation frames
// ----------------
static cocos2d::Vector<cocos2d::SpriteFrame*> getCha_kni_fAnimationFrames();
// ---------------
// animate actions
// ---------------
static cocos2d::Animate *createCha_kni_fAnimateAction(float delay, unsigned int loops = 1);
}; // class
}; // namespace
#endif // __KNIGHT_H__
然后把贴图贴到场景里去
打开 GameScene.cpp
bool GameScene::init() {
if (!Layer::init())
{
return false;
}
//get scene size
auto visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
...
return true;
}
把图片添加到场景初始化方法中
auto knightCache = new TexturePacker::Knight;
knightCache->addSpriteFramesToCache();
实例化Knight类 因为它在TexturePacker空间命名中,所以需要TexturePacker::Knight
先缓存需要的文件,这里knight.cpp已经封装好了,直接用他静态方法 addSpriteFramesToCache()
Sprite *knight = knightCache->createCha_kni_f_0000Sprite();
knight->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
this->addChild(knight);
定义一个类型为 Sprite 的 knight 指针,指向 knightCache 内的
Sprite * Knight::createCha_kni_f_0000Sprite();
最后添加到this,也就是GameScene
让图片动起来,需要把一个序列的图片按顺序添加进去,
TexturePacker已经封装好了,Knight::getCha_kni_fAnimationFrames(),
Animate *knightAnimation = knightCache->createCha_kni_fAnimateAction(0.14f);
knight->runAction(RepeatForever::create(knightAnimation));
定义一个类型为 Animate 的 knightAnimation 指针,指向 knightCache 内的
Animate *Knight::createCha_kni_fAnimateAction();
属性和方法的使用,参阅官方文档。