HouseMapDecoder.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. var HouseData = require("HouseData");
  2. var HouseMapDecoder = cc.Class({
  3. extends: cc.Component,
  4. properties: {
  5. jsonName:"house",
  6. houseList:{
  7. default:[],
  8. type:[HouseData],
  9. },
  10. },
  11. //解析数据
  12. DecodeJson:function (event) {
  13. //cc.log("===解析房屋数据===");
  14. var self = this;
  15. self.reCb = event;
  16. cc.loader.loadRes("json/"+self.jsonName, function (error, obj) {
  17. if(error)
  18. {
  19. //cc.log("+++解析出错,查下json+++" + error);
  20. self.reCb(false);
  21. return;
  22. }
  23. var jsonRoot = obj.json.house;
  24. //cc.log("===数据长度===" + jsonRoot.length);
  25. for (var i = 0; i < jsonRoot.length; i++) {
  26. var houseD = new HouseData();
  27. houseD.Id = jsonRoot[i].Id;
  28. houseD.icon = jsonRoot[i].icon;
  29. houseD.name = jsonRoot[i].name;
  30. houseD.price = jsonRoot[i].price;
  31. houseD.addBonus = jsonRoot[i].addBonus;
  32. houseD.addHp = jsonRoot[i].addHp;
  33. self.houseList[i] = houseD;
  34. }
  35. self.reCb(true);
  36. });
  37. },
  38. //通过名字拿到当前的数据 不建议用,你要用我也没办法
  39. getDataByName:function (name) {
  40. var data = null;
  41. for (var i = this.houseList.length - 1; i >= 0; i--) {
  42. if(name == this.houseList[i].name)
  43. {
  44. data = this.houseList[i];
  45. break;
  46. }
  47. }
  48. return data;
  49. },
  50. //通过itemid获取数据
  51. getDataByItemId:function(itemId){
  52. var data = null;
  53. for (var i = this.houseList.length - 1; i >= 0; i--) {
  54. if(itemId == this.houseList[i].Id)
  55. {
  56. data = this.houseList[i];
  57. break;
  58. }
  59. }
  60. return data;
  61. },
  62. getJsonLength:function(){
  63. return this.houseList.length
  64. },
  65. getDataList:function(){
  66. return this.houseList;
  67. },
  68. });
  69. module.exports = HouseMapDecoder;