一对一源码,滑块验证,滑块左右滑动显示的位置实现的相关代码
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);
}
}
以上就是一对一源码,滑块验证,滑块左右滑动显示的位置实现的相关代码, 更多内容欢迎关注之后的文章