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

120 lines
3.6 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
},
touchNode: {
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;
self.moveLeft();
}
})
},
start() {
// this.touchNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
// this.touchNode.on(cc.Node.EventType.TOUCH_START, this.onTouchBg, this);
},
// 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/part4/00' + 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, -750, 13);
var seq = cc.sequence(moveCurrent, moveFinish);
self.currentNode.runAction(seq);
self.rightNode.runAction(cc.moveTo(0.5, 0, 13));
self.leftNode.position = cc.v2(750, 13);
},
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(-750, 13);
let tmp = self.rightNode;
self.rightNode = self.currentNode;
self.currentNode = self.leftNode;
self.leftNode = tmp;
let url = 'textures/part4/00' + 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, 750, 13);
var seq = cc.sequence(moveCurrent, moveFinish);
self.currentNode.runAction(seq);
self.leftNode.runAction(cc.moveTo(0.5, 0, 13));
},
onTouchBg(e) {
cc.log(e.getLocation());
let location = e.getLocation();
this.startX = location.x;
},
onTouchEnd(e) {
cc.log('touch end');
let location = e.getLocation();
if (location.x - this.startX > 10) {
this.moveRight();
} else if (location.x - this.startX < -10) {
this.moveLeft();
}
},
});