首页 > 代码库 > Node.JS 文件读写,把Sheet图集转换为龙骨动画图集

Node.JS 文件读写,把Sheet图集转换为龙骨动画图集


Node.JS 文件读写,把Sheet图集数据转换为龙骨动画图集数据
  1. var fs = require("fs")
  2. var readline = require("readline");
  3. var rl = readline.createInterface({
  4. input:process.stdin,
  5. output:process.stdout
  6. });
  7. var path = undefined;
  8. var dbName = undefined;
  9. rl.question("请输入文件名",function(answer){
  10. path = answer;
  11. rl.question("请输入龙骨动画名",function(answer){
  12. dbName = answer;
  13. readAndWriteFile();
  14. rl.close();
  15. })
  16. });
  17. var convert = function(sheetData){
  18. var data = {};
  19. data["imagePath"] = path + ".png";
  20. data["name"] = dbName;
  21. data["SubTexture"] = [];
  22. var frames = sheetData.frames
  23. for(var index in frames){
  24. var frameData = frames[index];
  25. var d = {};
  26. d.name = index;
  27. d.height = frameData.h;
  28. d.width = frameData.w;
  29. d.x = frameData.x;
  30. d.y = frameData.y;
  31. data["SubTexture"].push(d);
  32. }
  33. return data;
  34. }
  35. var readAndWriteFile = function(){
  36. fs.readFile(path + ".json", function (err, data) {
  37. if (err) {
  38. return console.error(err);
  39. }
  40. var d = JSON.parse(data);
  41. if(d["SubTexture"] !== undefined){
  42. console.log(path + "已经转换");
  43. return;
  44. }
  45. var newData = convert(d);
  46. fs.writeFile(path + ".json", JSON.stringify(newData), function(err) {
  47. if (err) {
  48. return console.error(err);
  49. }
  50. });
  51. });
  52. }



null


Node.JS 文件读写,把Sheet图集转换为龙骨动画图集