// Learn cc.Class: // - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html // - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html // Learn Attribute: // - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html // - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html // Learn life-cycle callbacks: // - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html // - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html cc.Class({ extends: cc.Component, properties: { moveDir: 1, //移动方向 1为向右 -1为向左 moveTime: 0, //移动时间 moveDistance: 0,//移动距离 haveProp: 0 }, // LIFE-CYCLE CALLBACKS: onLoad: function () { }, initProperties: function (configure) { this.moveDistance = configure.distance; this.moveTime = configure.time; this.moveDir = configure.dir; for (let i = 0; i < this.node.children.length; i++) { var circle = this.node.getChildByName("t" + i); circle.colorType = Math.floor(i / 4); } this.startActiton(); }, startActiton: function () { var moveBy = cc.moveBy(this.moveTime, cc.p(this.moveDistance * this.moveDir, 0)); this.node.runAction(cc.repeatForever(moveBy)); }, pauseAction: function () { this.node.pauseAllActions(); }, resumeAction: function () { this.node.resumeAllActions(); }, start() { }, update: function (dt) { if (this.moveDir == 1) { if (this.node.x >= (this.node.width / 2 + cc.winSize.width / 2)) { this.node.position = cc.p(-(this.node.width / 2 + this.node.width - cc.winSize.width / 2), this.node.y); } } else { if (this.node.x <= -(this.node.width / 2 + cc.winSize.width / 2)) { this.node.position = cc.p((this.node.width / 2 + this.node.width - cc.winSize.width / 2), this.node.y); } } }, });