我在ToggleParticle里面加了一个progressBar,想实现当进度条满的时候,点击触发粒子的发射,粒子发射后进度条清零重新填满。功能实现了,但是里面有个关于this作用域的问题很奇怪,希望哪位大神可以解答下。
下面是代码:
cc.Class({
extends: cc.Component,
properties: {
particle: {
default: null,
type: cc.Node
},
progressBar:{
default:null,
type:cc.ProgressBar
},
speed:5,
},
onLoad: function () {
this.progressBar.progress = 0;
this.ready = false;
this.setInputControl();
},
setInputControl: function(progressBar) {
_ var self = this;_
_ this.node.on(‘mousedown’, function(){_
_ console.log(‘mousedown’);_
_ console.log(self);_
_ console.log(this);_
_ self.toggleParticlePlay();_
_ self.progressBar.progress=0;_
_ }.bind(this)); _
_ },_
toggleParticlePlay: function() {
var myParticle = this.particle.getComponent(cc.ParticleSystem);
if (myParticle.isFull()) { // check if particle has fully plaed
myParticle.stopSystem(); // stop particle system
} else {
myParticle.resetSystem(); // restart particle system
}
},
update: function (dt) {
this._updateProgressFull(this.progressBar,dt);
if (this.ready) {
this.setInputControl(this.progressBar);
this.ready = false;
}
},
_updateProgressFull: function (progressBar,dt) {
var progress = progressBar.progress;
if(progress<1.0 && !this.ready) {
progress +=dt*this.speed;
progressBar.progress = progress;
} else if (progress>=1) {
progressBar.progress=1;
this.ready=true;
}
},
});
