invite_page/assets/scripts/bottomSwiper.js
2019-02-28 21:34:11 +08:00

100 lines
3.0 KiB
JavaScript

cc.Class({
extends: cc.Component,
properties: {
leftBtn: {
default: null,
type: cc.Node
},
rightBtn: {
default: null,
type: cc.Node
},
currentNode: {
default: null,
type: cc.Node
},
leftNode: {
default: null,
type: cc.Node
},
rightNode: {
default: null,
type: cc.Node
},
currentIdx: 2,
scrolling: false
},
// LIFE-CYCLE CALLBACKS:
onLoad() {
let self = this;
this.leftBtn.on('click', () => {
if (!self.scrolling) {
self.scrolling = true;
self.moveRight();
}
});
this.rightBtn.on('click', () => {
if (!self.scrolling) {
self.scrolling = true;
this.moveLeft();
}
})
},
start() {
},
// update (dt) {},
moveLeft: function () {
let self = this;
let moveFinish = cc.callFunc(function () {
self.currentIdx ++;
if (self.currentIdx > 4) {
self.currentIdx = 0;
}
let nextIdx = self.currentIdx === 4 ? 0 : self.currentIdx + 1;
let tmp = self.leftNode;
self.leftNode = self.currentNode;
self.currentNode = self.rightNode;
self.rightNode = tmp;
let url = 'textures/part5/0' + nextIdx;
cc.loader.loadRes(url, cc.SpriteFrame, function (err, spriteFrame) {
self.rightNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
});
self.scrolling = false;
}, this, {});
let moveCurrent = cc.moveTo(0.5, -561, 0);
var seq = cc.sequence(moveCurrent, moveFinish);
self.currentNode.runAction(seq);
self.rightNode.runAction(cc.moveTo(0.5, 0, 0));
self.leftNode.position = cc.v2(561, 0);
},
moveRight: function () {
let self = this;
let moveFinish = cc.callFunc(function () {
self.currentIdx --;
if (self.currentIdx < 0) {
self.currentIdx = 4;
}
let nextIdx = self.currentIdx === 0 ? 4 : self.currentIdx - 1;
self.rightNode.position = cc.v2(-561, 0);
let tmp = self.rightNode;
self.rightNode = self.currentNode;
self.currentNode = self.leftNode;
self.leftNode = tmp;
let url = 'textures/part5/0' + nextIdx;
cc.loader.loadRes(url, cc.SpriteFrame, function (err, spriteFrame) {
self.leftNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
});
self.scrolling = false;
}, this, {});
let moveCurrent = cc.moveTo(0.5, 561, 0);
var seq = cc.sequence(moveCurrent, moveFinish);
self.currentNode.runAction(seq);
self.leftNode.runAction(cc.moveTo(0.5, 0, 0));
}
});