UtilsPrefabs.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. window.UtilsPrefabs = {
  2. init : function (parentNode) {
  3. this.parentNode = parentNode;
  4. return this;
  5. },
  6. getPrefabsName : function(){
  7. return this.prefabsName;
  8. },
  9. addPrefabs : function (prefabsName,parentNode,callback) {
  10. cc.loader.loadRes(prefabsName, function (err, texture) {
  11. var node = this.parentNode;
  12. if (parentNode!=null) {
  13. node = parentNode;
  14. }
  15. var prefab = cc.instantiate(texture);
  16. this.prefabsName = prefab.name;
  17. this.removePrefabs(node,this.prefabsName);
  18. if (node!=null) {
  19. node.addChild(prefab);
  20. }else{
  21. return;
  22. }
  23. if (callback != null) {
  24. callback(prefab);
  25. }
  26. }.bind(this));
  27. },
  28. removePrefabs : function (parentNode,prefabsName,success,fail) {
  29. var node = this.parentNode;
  30. if (parentNode!=null) {
  31. node = parentNode;
  32. }
  33. if (node!=null) {
  34. if (node.getChildByName(prefabsName) != null) {
  35. node.getChildByName(prefabsName).off(cc.Node.EventType.TOUCH_END);
  36. node.getChildByName(prefabsName).destroy();
  37. if (success != null) {
  38. success();
  39. }
  40. }else{
  41. if (fail != null) {
  42. fail();
  43. }
  44. }
  45. }
  46. },
  47. setOff : function(node){
  48. node.off(cc.Node.EventType.TOUCH_END);
  49. },
  50. setOn : function(node,callback){
  51. if (node != null) {
  52. node.on(cc.Node.EventType.TOUCH_END,callback);
  53. }else{
  54. console.log("当前被放进来的node节点没有可以点击的名字 请关注此段警告");
  55. }
  56. return this;
  57. },
  58. getNode: function (name, parent) {
  59. if (parent == null) {
  60. if (this.parentNode.getChildByName(name) == null) {
  61. // console.log("在",this.parentNode,"没有找到名字为"+name+"的node");
  62. }
  63. return this.parentNode.getChildByName(name);
  64. } else {
  65. if (parent.getChildByName(name) == null) {
  66. // console.log("在",parent,"没有找到名字为"+name+"的node");
  67. }
  68. return parent.getChildByName(name);
  69. }
  70. },
  71. loadResSpriteFrame : function(src,callback){
  72. cc.loader.loadRes(src,cc.SpriteFrame, function (err, texture) {
  73. if (callback!=null) {
  74. callback(texture);
  75. }
  76. }.bind(this));
  77. },
  78. }