• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Multiple variables for event listeners. Javascript

Community Beginner ,
Aug 17, 2022 Aug 17, 2022

Copy link to clipboard

Copied


I have many elements, movieclips, on the stage. I assigned them to the array.

const clickEvent = [{
	product: [exportRoot.ham, exportRoot.yeast, exportRoot.milk, exportRoot.milk2, exportRoot.cheese, exportRoot.flour, exportRoot.flour2]
	product_ob: [exportRoot.shopcart.ham_ob, exportRoot.shopcart.yeast_ob, exportRoot.shopcart.milk_ob, exportRoot.shopcart.milk2_ob, exportRoot.shopcart.cheese_ob, exportRoot.shopcart.flour_ob, exportRoot.shopcart.flour2_ob]
	//selling:
	//buying:
}]

clickEvent.product[index].addEventListener("click",  (e) => {
    this.shopcart.product_ob[index].play();
    playSound('hint');
}); //(Did not work)


Movieclip "shopcart" contains food products that are animated when you press the corresponding buttons on the stage. I would like to write this in a function, whatever will keep the code to a minimum.
Maybe something like that:

	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: 10,
},{
	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: 10,

}]


for (var i = 0; i < right.length; i++){ 
	right[i].button2.visible = false
}


All crap code: 

this.shopcart.ham_ob.on("mouseover", function (e) {
	this.cursor = "pointer";
});
this.shopcart.yeast_ob.on("mouseover", function (e) {
	this.cursor = "pointer";
});
this.shopcart.milk_ob.on("mouseover", function (e) {
	this.cursor = "pointer";
});
this.shopcart.cheese_ob.on("mouseover", function (e) {
	this.cursor = "pointer";
});
this.shopcart.flour_ob.on("mouseover", function (e) {
	this.cursor = "pointer";
});

//////////////////////////////////////////////////////////////
this.ham.addEventListener("click",  (e) => {
	this.shopcart.ham_ob.play();
	playSound('hint');
});
this.shopcart.ham_ob.addEventListener("click",  (e) => {
	this.shopcart.ham_ob.play();
	this.cursor = "pointer";
	playSound('hint');
});


this.yeast.addEventListener("click",  (e) => {
	this.shopcart.yeast_ob.play();
	playSound('hint');
});
this.shopcart.yeast_ob.addEventListener("click",  (e) => {
	this.shopcart.yeast_ob.play();
	playSound('hint');
});


this.milk.addEventListener("click",  (e) => {
	this.shopcart.milk_ob.play();
	playSound('hint');
});
this.shopcart.milk_ob.addEventListener("click",  (e) => {
	this.shopcart.milk_ob.play();
	playSound('hint');
});


this.cheese.addEventListener("click",  (e) => {
	this.shopcart.cheese_ob.play();
	playSound('hint');
});
this.shopcart.cheese_ob.addEventListener("click",  (e) => {
	this.shopcart.cheese_ob.play();
	playSound('hint');
});


this.flour.addEventListener("click",  (e) => {
	this.shopcart.flour_ob.play();
	playSound('hint');
});
this.shopcart.flour_ob.addEventListener("click",  (e) => {
	this.shopcart.flour_ob.play();
	playSound('hint');
});


Thanks for any help!

TOPICS
Code , Error , Missing feature

Views

86

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2022 Aug 18, 2022

Copy link to clipboard

Copied

LATEST

// note clickEvent is an object with two elements, not an array

const clickEvent = {
product: [exportRoot.ham, exportRoot.yeast, exportRoot.milk, exportRoot.milk2, exportRoot.cheese, exportRoot.flour, exportRoot.flour2],
product_ob: [exportRoot.shopcart.ham_ob, exportRoot.shopcart.yeast_ob, exportRoot.shopcart.milk_ob, exportRoot.shopcart.milk2_ob, exportRoot.shopcart.cheese_ob, exportRoot.shopcart.flour_ob, exportRoot.shopcart.flour2_ob]
//selling:
//buying:
}

 

for(var i=0;i<clickEvent.product.length;i++){
clickEvent.product[i].addEventListener("click", clickF.bind(i));
}


function clickF(){
clickEvent.product_ob[this].play();
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines