pondLayer.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. // foo: {
  14. // // ATTRIBUTES:
  15. // default: null, // The default value will be used only when the component attaching
  16. // // to a node for the first time
  17. // type: cc.SpriteFrame, // optional, default is typeof default
  18. // serializable: true, // optional, default is true
  19. // },
  20. // bar: {
  21. // get () {
  22. // return this._bar;
  23. // },
  24. // set (value) {
  25. // this._bar = value;
  26. // }
  27. // },
  28. },
  29. // LIFE-CYCLE CALLBACKS:
  30. onLoad () {
  31. this.index = 0;
  32. this.count = 0;
  33. this.flora = null;
  34. },
  35. /**
  36. * 加载精灵
  37. * @param {*} index
  38. */
  39. loadSprite: function(index) {
  40. var node = cc.instantiate(this.flora);
  41. cc.log("------------index-----------: " , index);
  42. let self = this;
  43. var name = 'bg/bg' + index;
  44. cc.loader.loadRes(name, cc.SpriteFrame, function(err, spFrame) {
  45. // if (self.index == 3) {
  46. // self.index = 0;
  47. // ++index;
  48. // }
  49. if (self.count == 5) return;
  50. if (err) {
  51. cc.log(err.message || err);
  52. return;
  53. }
  54. let node = new cc.Node('newNode');
  55. const sprite = node.addComponent(cc.Sprite);
  56. sprite.spriteFrame = spFrame;
  57. node.anchorY = 0;
  58. node.anchorX = 0;
  59. node.x = 0;
  60. node.y = -self.node.getContentSize().height * self.count;
  61. self.node.addChild(node);
  62. // ++self.index;
  63. ++self.count;
  64. self.loadSprite(index);
  65. var flora = cc.instantiate(self.flora);
  66. flora.x = (self.count % 2 == 0 ) ? 100 : 500;
  67. flora.y = 300;
  68. node.addChild(flora);
  69. });
  70. },
  71. start () {
  72. },
  73. // update (dt) {},
  74. });