Skip to content

Instantly share code, notes, and snippets.

@bmanGH
Created June 8, 2014 16:50
Show Gist options
  • Save bmanGH/79b5fda766f4714777e3 to your computer and use it in GitHub Desktop.
Save bmanGH/79b5fda766f4714777e3 to your computer and use it in GitHub Desktop.
a node.js script to generate cocos2d spriteframe animation file
{
"name": "spriteframe_animation_generator",
"main": "./spriteframe_animation_generator",
"dependencies": {
"ejs": "1.0.0",
"sprintf-js" : "0.0.7"
}
}
var ejs = require('ejs');
var sprintf = require("sprintf-js").sprintf;
var fs = require('fs');
var path = require('path');
// exec
var dataFilePath = __dirname + '/' + process.argv[2]; // 动画元数据路径
fs.readFile(dataFilePath, 'utf8', function (err, data) { // 读取动画元数据
if (err)
throw err;
var ejsData = {};
ejsData.animList = []; // 动画列表
// 动画元数据结构
// 第一行是spritesheet文件名
// [动画名];[FPS];[起始帧索引];[动画帧数];[动画名前缀]\n
data.split('\n').forEach (function (dataLine) {
if (ejsData.spritesheetName == undefined) // 读取第一行数据为spritesheet文件名
{
ejsData.spritesheetName = dataLine;
return;
}
var dataSplit = dataLine.split(';');
var animData = {};
animData.animName = dataSplit[0];
animData.delayPerUnit = 1.0 / parseFloat(dataSplit[1]);
var animStartFrameIndex = parseInt(dataSplit[2]);
var animFrameCount = parseInt(dataSplit[3]);
animData.frameList = [];
for (var i = 0; i < animFrameCount; i++) { // 拼接动画帧名
var frameName = sprintf("%s/%04d", dataSplit[4], animStartFrameIndex + i);
animData.frameList.push(frameName);
}
ejsData.animList.push(animData);
});
var ejsTemplate = fs.readFileSync('template.ejs', 'utf8'); // 读取模板
var result = ejs.render(ejsTemplate, ejsData); // 输出结果
var targetPath = path.dirname(dataFilePath) + '/' + path.basename(dataFilePath, '.txt') + '.plist';
fs.writeFile(targetPath, result, 0, 'utf8', function (err) {
if(err)
throw err;
});
});
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>animations</key>
<dict>
<% animList.forEach(function(animData) { %>
<key><%- animData.animName %></key>
<dict>
<key>delayPerUnit</key>
<real><%- animData.delayPerUnit %></real>
<key>frames</key>
<array>
<% animData.frameList.forEach(function(frameName) { %>
<dict>
<key>delayUnits</key>
<real>1</real>
<key>notification</key>
<dict/>
<key>offset</key>
<string>{0, 0}</string>
<key>spriteframe</key>
<string><%- frameName %></string>
</dict>
<% }); %>
</array>
<key>loop</key>
<false/>
<key>loops</key>
<integer>1</integer>
<key>maxRandomTime</key>
<real>0.10000000149011612</real>
<key>minRandomTime</key>
<real>0.10000000149011612</real>
<key>randomFrames</key>
<false/>
<key>randomReplay</key>
<false/>
<key>restoreOriginalFrame</key>
<false/>
</dict>
<% }); %>
</dict>
<key>properties</key>
<dict>
<key>format</key>
<integer>2</integer>
<key>spritesheets</key>
<array>
<string><%- spritesheetName %></string>
</array>
</dict>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment