现在我想实现一个功能,就是按住屏幕不放,然后显示当前力度大小(按住屏幕越久力度越大但在一个范围内由小到大然后到最大值后置为0)! 在touchstart事件里触发
那就实现呗
我用touchstart和touchend实现了长按,但是不是我要的效果,我想的是在触摸开始就开始计数
/**
* 把这个脚本挂在按钮上面就OK
* this.node = 按钮
* this.progressBar = 进度条
*/
cc.Class({
extends: cc.Component,
properties: {
progressBar: cc.Sprite,
},
onLoad() {
this.initData();
this.initFunc();
},
initData() {
this.isMove = false;
},
initFunc() {
this.node.on('touchstart', () => {
this.isMove = true;
})
this.node.on('touchend', () => {
this.isMove = false;
this.progressBar.fillRange = 0;
})
},
update() {
if (this.isMove) {
let bar = this.progressBar;
if (bar.fillRange >= 1) {
bar.fillRange -= 0.01;
} else if (bar.fillRange <= 0) {
bar.fillRange += 0.01;
}
}
},
});
好的! 我研究研究 看下原理
兄台,这个方法我试过了,他是在触摸结束后发生的,就像上面我说的那样,这两个监听是实现了长按,但是我想的是在按下就发生效果,并不需要他监听到触摸结束!就是说我在长按的时候会有表示力度的数字在变化,触摸一次屏幕不做操作或做其他提示。 触摸结束的时候返回这个力度的数值大小
你在update里面处理,就可以实现你不需要触摸结束的需求。
这不叫请教思路。。这叫 思路有了 就差代码了。。
多谢兄台提醒, onLoad () {
let self=this;
self.node.on(‘touchmove’,function(event){
self.bars.progress+=0.1;
if(self.bars.progress>1){
self.bars.progress=0;
}
console.log(self.bars.progress);
},self);
}
我用移动来做了,这样在移动范围内滑动 进度条就是变动的,然后在完善下
就是这样的啊 当你按下去的那一瞬间 触发了touchstart 就开始在update进行进度条的来回加减 当你放下的时候 就会触发touchend 这个时候的会对进度条初始化为0 你可以拿到 this.progressBar.fillRange 的值 假设你现在的 力度上限为100
当前的力度就是 100 * this.progressBar.fillRange 实时显示力度在update1里面处理 松开按钮在touchend里面处理
手把手教写代码。。
还有一个 用定时器,start的时候调用,然后改变数值,end的时候关闭在返回值
update (dt) {
if(this.istouch){
this.bars.progress+=0.1;
if(this.bars.progress>1){
this.bars.progress=0;
}
}
}
就是处理的写在update里嘛!
onLoad () {
let self=this;
self.node.on(‘touchstart’,function(){
this.istouch=true;
// console.log(self.bars.progress);
},self);
self.node.on('touchend',function(){
this.istouch=false;
console.log(self.bars.progress);
},self);然后结束返回
谢了 ,各位大佬,最开始的数值处理放在了end监听里,放在updat里就对了,还新增了其他两种实现方法!