One-to-one source code, slider verification, the relevant code of the slider displayed by sliding left and right
const {
ccclass,
property
} = cc._decorator;
@ccclass
export default class TestPanel extends cc.Component {
/**
* 鼠标按下位置
*
* @private
* @type {cc.Vec2}
* @memberof TestPanel
*/
private clickPos: cc.Vec2;
start() {
this.node.on(cc.Node.EventType.TOUCH_START, this.ClickDown, this);
this.node.on(cc.Node.EventType.TOUCH_END, this.ClickUp, this);
}
ClickDown(event: cc.Event.EventMouse) {
this.clickPos = event.getLocation();
}
ClickUp(event: cc.Event.EventMouse) {
if (this.clickPos == null) {
return;
}
//比较开始坐标和结束坐标
// console.log("起始位置:"+this.clickPos);
// console.log("结束位置:"+event.getLocation());
// let distance = this.clickPos.sub(event.getLocation()).mag();
let distance = Math.abs(event.getLocation().x - this.clickPos.x);
console.log("滑动距离distance:" + distance);
if (this.clickPos.x < event.getLocation().x) {
if (distance > 250) {
console.log("从左往右滑动" + this.centerParent.position.x);
}
} else if (this.clickPos.x > event.getLocation().x) {
if (distance > 250) {
console.log("从右往左滑动" + this.centerParent.position.x);
}
} else {
//原地不动
}
}
onDestroy(){
this.node.off(cc.Node.EventType.TOUCH_START, this.ClickDown, this);
this.node.off(cc.Node.EventType.TOUCH_END, this.ClickUp, this);
}
}
The above is the relevant code of the one-to-one source code, slider verification, and the position of the slider displayed by sliding left and right. For more content, please pay attention to future articles