有个小人在中间跑,手指滑动可以移动位置,默认在中间
move方法删了,就用end就行。x>0右移,x<0左移。
哈哈哈哈哈哈,那不行,end要松开才知道结果,会有"延迟"的。。
你这个是只有在三个跑道跑还是会在任意X值跑?
你这个不是没有中间,是因为move触发的次数太多,导致你这里看起来是直接从左跳到了右或者从右跳到了左
你在touch_move加个打印就会发现,move会一直触发的
不是很明白你这个currentX是想表达什么意思?是不是手指移动50px就换一格?
对,手指移动50px就换一格,我打印出来0左,1中,2右,是会走到中间的
但是我不知道怎么在updata里停下来=。=!
只有3个跑道,左中右
加一个变量,变成1了就return,只有touchend了变量改变状态。
顺便问一句,看头像,目测是妹子,猜的对吗
那你不能用update,应该用tween。还是用touchend
//调用该方法,英雄可以断续滑动移动
_bindHeroMoveEvent() {
this.node.on(cc.Node.EventType.TOUCH_END, e => {
const _dPos = e.getLocation().sub(e.getStartLocation());
const _isHori = Math.abs(_dPos.x / _dPos.y) > 1;
if (_isHori) {
if (Math.abs(_dPos.x) > TOUCH_THRESHOLD) return this._hroiMoveHero(_dPos.x > 0);
} else {
if (Math.abs(_dPos.y) > TOUCH_THRESHOLD) return this._vertMoveHero(_dPos.y > 0);
}
})
},
//英雄断续水平移动
_hroiMoveHero(isToRight) {
if (this._heroHoriMoveLock) return;
let _targetXScale = this._heroHoriXScale + (isToRight ? 1 : -1);
if (_targetXScale > 0) _targetXScale = 1;
else if (_targetXScale < 0) _targetXScale = -1;
this._heroHoriXScale = _targetXScale;
this._heroHoriMoveLock = true;
cc.tween(this._hero).to(0.1, { x: _targetXScale * this._heroHoriStepSize })
.call(() => this._heroHoriMoveLock = false)
.start();
},
你这个阈值设的有问题。从代码中看,往左是30像素就跳转一格,往右是0像素就跳转?这个阈值可以调整试一下。然后就是再加个逻辑,在跳转之后,将touchX重置为当前点的坐标(这个应该是你存的初始点击的x坐标吧?)
TouchMove(touch: Touch,event: EventTouch) {
if(!this.isActive){
return;
}
let endX=event.getLocation().x;
let dis =this.touchX-endX
let checkX = 60; // 阈值,自己调整试下
//滑动距离
if (dis > checkX) { //左边
this.currentX-=50;
this.touchX = endX;
}else if (dis < -checkX) { //右边
this.currentX+=50;
this.touchX = endX;
}
}
稍微改了下你试试
我的小人还要往前走的,Z轴上还要走(自动的,一直走),像跑酷那种哦,不放在updata里,不行吧?
还有个问题,你边界限制放在update里了,这块应该提上来放在move里
//滑动距离
if (dis > checkX) { //左边
this.currentX-=50;
if (this.currentX <= -50) this.currentX = -50;
this.touchX = endX;
}else if (dis < -checkX) { //右边
this.currentX+=50;
if (this.currentX >= 50) this.currentX = 50;
this.touchX = endX;
}
那也可以啊。z轴使用update。x轴用touchend。
嗯嗯,end是没问题的,不过感觉有点“延迟”,move应该就能像“地铁跑酷”那种了
move按理说应该也没问题啊。
当前状态要么是左,要么是中,要么是右。
move判断如果大于向左阈值,如果是中则到左。如果是右则到中。然后更新状态。
,那个是是全局变量,在move里拿到值,然后在updata里做判断位移,一样的!不知道是不是updata跟move一直执行会有问题。。反正end没事,恍恍惚惚。。
试试把阈值加大,我只是随手设的60,实际60px很小,具体看你设计尺寸来,设到1/4的宽度看看

