// Learn cc.Class: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html // Learn Attribute: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html cc.Class({ extends: cc.Component, properties: { // foo: { // // ATTRIBUTES: // default: null, // The default value will be used only when the component attaching // // to a node for the first time // type: cc.SpriteFrame, // optional, default is typeof default // serializable: true, // optional, default is true // }, // bar: { // get () { // return this._bar; // }, // set (value) { // this._bar = value; // } // }, }, // LIFE-CYCLE CALLBACKS: onLoad () { this.index = 0; this.count = 0; this.flora = null; }, /** * 加载精灵 * @param {*} index */ loadSprite: function(index) { var node = cc.instantiate(this.flora); cc.log("------------index-----------: " , index); let self = this; var name = 'bg/bg' + index; cc.loader.loadRes(name, cc.SpriteFrame, function(err, spFrame) { // if (self.index == 3) { // self.index = 0; // ++index; // } if (self.count == 5) return; if (err) { cc.log(err.message || err); return; } let node = new cc.Node('newNode'); const sprite = node.addComponent(cc.Sprite); sprite.spriteFrame = spFrame; node.anchorY = 0; node.anchorX = 0; node.x = 0; node.y = -self.node.getContentSize().height * self.count; self.node.addChild(node); // ++self.index; ++self.count; self.loadSprite(index); var flora = cc.instantiate(self.flora); flora.x = (self.count % 2 == 0 ) ? 100 : 500; flora.y = 300; node.addChild(flora); }); }, start () { }, // update (dt) {}, });