I am having some issues figuring out why this code still lets btn_1
call the function setstate3()
.
There is a function update()
with a code check for (setstate == X)
within. When it gets to if (setstate == 5)
and if (setstate == 6)
, I want to remove the listeners.
The story ends that that point and will need a restart or whatever - for testing I want those buttons to be inactive at this point.
If I just turn off both listeners at if (setstate == 1)
, the button becomes inactive. However, for some reason that doesn't work if I do so at if (setstate == 5)
or if (setstate == 6)
in the code. I expected it to work if you removed the listener inside the function it calls in the first place. It's not even throwing any error at all.
I did some research and had different attempts at cloning the element (apparently that will kill all listeners on an object), but I can't get that to be recognised as a command.
Any help would be greatly appreciated - have a great day
class Game {
constructor (exportRoot, stage) {
this.root = exportRoot
this.stage = stage
this.str1 = "start"
this.strOp1 = "1"
this.strOp2 = "2"
this.init()
this.setstate
this.randomNumber
}
init () {
const game = this
this.main_Txt = this.root.main_Txt
this.op_1 = this.root.op_1
this.op_2 = this.root.op_2
this.root.start_bt.on('click', function () { game.update() })
this.setstate = 0
};
update () {
if (this.setstate == 0) {
this.str1 = 'You have had a really big weekend camping. In fact as you are driving home through the dark forest you can hardly keep you eyes open. The stereo is playing your favourite song but suddenly it cuts out. There is a huge bright purple light in the sky and your car sputters to a stop. The stereo is dead and all the lights are out.'
this.strOp1 = 'Try to fix your car'
this.strOp2 = 'Start walking'
this.root.start_bt.visible = 0
this.root.mc_2.visible = false
this.root.btn_1.on('click', function () { game.setstate1()})
//this.root.btn_2.on("click", function(){ game.setstate2(); })
} else if (this.setstate == 1) {
this.str1 = 'You get out and open the hood, its hard to see but there is still some purple glow in the sky. You wonder what that big light came from and what is going on. All of a sudden there is some moaning and some noises from the underbrush and a zombie stumbles out onto the road.'
this.strOp1 = 'Fight the zombie'
this.strOp2 = 'Ask him to help fix the car'
this.root.mc_1.visible = false
this.root.mc_2.visible = true
this.root.btn_1.on('click', function() { game.setstate3() })
//this.root.btn_2.on("click", function(){ game.setstate2(); })
} else if (this.setstate == 5) {
this.str1 = 'You try to fight but the zombie overpowers you and eats your brain.'
this.strOp1 = "dead"
this.strOp2 = "dead"
this.root.mc_1.visible = false
this.root.mc_2.visible = false
//this.root.btn_2.on("click", function(){game.setstate2(); })
} else if (this.setstate == 6) {
this.str1 = "lost"
this.strOp1 = "dead"
this.strOp2 = "dead"
this.root.mc_1.visible = false
this.root.mc_2.visible = false
//this.root.btn_2.on("click", function(){ game.setstate2(); }
}
this.setText();
}
chooseNumber () {
this.randomNumber = Math.floor(Math.random() * 10) + 1
}
setText () {
this.main_Txt.text = this.str1;
this.op_1.text = this.strOp1;
this.op_2.text = this.strOp2;
}
setstate1 () {
this.root.btn_1.off('click', function (){ game.setstate1() })
console.log('setstate1')
this.setstate = 1
this.update();
}
setstate3 () {
this.root.btn_1.off('click', function (){ game.setstate3() })
this.chooseNumber();
if (this.randomNumber >= 5) {
console.log('setstate5');
this.setstate = 5;
this.update();
} else {
console.log('setstate6');
this.setstate = 6
this.update()
}
}
}
button.on
andbutton.off
functions come form? Usually you set button event listeners in Javascript withbutton.onclick
orbutton.addEventlistener
. Looks like you are using some framework you forgot to mention. \$\endgroup\$this.root.btn_1.on('click', function () { game.setstate1()})
you can probably just writethis.root.btn_1.on('click', game.setstate1)
. If you want an event listener to do nothing but call a single function with no arguments, you can just use that function as the event listener. \$\endgroup\$