Copy link to clipboard
Copied
My code is very simple, so I wonder how to change it so that when one object is pressed, the other object, and first one, is blocked. A game is about buying products in a store. There are many products in this place. Two products repeat, one is cheaper, the other is more expensive. When the button is pressed ("button: this.milk"), the milk locks the mouse on the pressed object on the shelf. But I can pressing on the second object, ("button: this.milk2), and then it charges me two prices. Is it possible to somehow block the mouse on two objects at the same time, when the first one is already selected in the shopping cart?
User must have 5 products (ham, yeast, milk, cheese, flour) to prepare the pizza. (pizzaplayed) He also has a limited amount of money up to 40 coins.
Thank for help!
const right = [{
button: this.ham,
button2: this.shopcart.ham_ob,
price: 12,
}, {
button: this.yeast,
button2: this.shopcart.yeast_ob,
price: 2,
}, {
button: this.milk,
button2: this.shopcart.milk_ob,
price: 4,
}, {
button: this.milk2,
button2: this.shopcart.milk2_ob,
price: 14,
},{
button: this.cheese,
button2: this.shopcart.cheese_ob,
price: 6,
},{
button: this.flour,
button2: this.shopcart.flour_ob,
price: 6,
},{
button: this.flour2,
button2: this.shopcart.flour2_ob,
price: 16,
}]
right.forEach((element, index) =>{
element.button.addEventListener('click', (event) =>{
pizzaplayed+=1
counterWin+=right[index].price
this.score.text = counterWin;
console.log("points",pizzaplayed,"/5")
console.log("money",counterWin,"/40")
element.button2.mouseEnabled = true;
element.button2.visible = true;
element.button.mouseEnabled = false;
this.goodAnswer.play();
if (pizzaplayed===5 && counterWin <=40){
checkWin();
}
});
element.button2.addEventListener('click', (event) =>{
pizzaplayed-=1
counterWin-=right[index].price
this.score.text = counterWin;
console.log("points",pizzaplayed,"/5")
console.log("money",counterWin,"/0")
element.button.mouseEnabled = true;
element.button2.visible = false;
element.button2.mouseEnabled = false;
});
});
Copy link to clipboard
Copied
in your button listener function, add:
if( element.button.name.indexOf("2")==-1 && this[element.button.name+"2"] ){
this[element.button.name+"2"].mouseEnabled=false;
}
if(element.button.name.indexOf("2")>-1){
this[element.button.name.slice(0,-1)].mouseEnabled=false;
}