Skip to main content
Multoman
Inspiring
November 23, 2022
Answered

Code optimization

  • November 23, 2022
  • 2 replies
  • 874 views

I have four buttons. When you hover the mouse cursor over one of the buttons, an inscription appears in the text field, when the cursor goes beyond the button, the text disappears. The code looks very large, although parts of it are repeated. Is there any way to reduce (optimize) this code? 

My main project uses more than 50 buttons (That's a lot of code!)

 

 

function OUT(e: MouseEvent): void {
	Text field.text = "";
}


function text1(e: MouseEvent): void {
	Text field.text = "hello";
}
button_1.addEventListener(MouseEvent.MOUSE_OVER, text1);
button_1.addEventListener(MouseEvent.MOUSE_OUT, OUT);

function text2(e: MouseEvent): void {
	Text field.text = "how are you doing";
}

button_2.addEventListener(MouseEvent.MOUSE_OVER, text2);
button_2.addEventListener(MouseEvent.MOUSE_OUT, OUT);

function text3(e: MouseEvent): void {
	Text field.text = "WHERE DO YOU WORK?";
}

button_3.addEventListener(MouseEvent.MOUSE_OVER, text3);
button_3.addEventListener(MouseEvent.MOUSE_OUT, OUT);

function text4(e: MouseEvent): void {
	Text field.text = "goodbye";
}
button_4.addEventListener(MouseEvent.MOUSE_OVER, text4);
button_4.addEventListener(MouseEvent.MOUSE_OUT, OUT);

 

 

This topic has been closed for replies.
Correct answer Colin Holgate

This code assumes the output field is named Textfield, and that your buttons are named button_1 to button_50. The 'message' array would have the 50 sets of texts you want:

 

stage.addEventListener(MouseEvent.MOUSE_OVER, text);
stage.addEventListener(MouseEvent.MOUSE_OUT, OUT);

function text(e: MouseEvent): void {
	var messages = ["hello","how are you doing","WHERE DO YOU WORK?","goodbye"]
	var btnnumber = e.target.name.split("_")[1]
	if(btnnumber) Textfield.text = messages[btnnumber-1];
}

function OUT(e: MouseEvent): void {
	Textfield.text = "";
}

 

2 replies

Colin Holgate
Colin HolgateCorrect answer
Inspiring
November 23, 2022

This code assumes the output field is named Textfield, and that your buttons are named button_1 to button_50. The 'message' array would have the 50 sets of texts you want:

 

stage.addEventListener(MouseEvent.MOUSE_OVER, text);
stage.addEventListener(MouseEvent.MOUSE_OUT, OUT);

function text(e: MouseEvent): void {
	var messages = ["hello","how are you doing","WHERE DO YOU WORK?","goodbye"]
	var btnnumber = e.target.name.split("_")[1]
	if(btnnumber) Textfield.text = messages[btnnumber-1];
}

function OUT(e: MouseEvent): void {
	Textfield.text = "";
}

 

Multoman
MultomanAuthor
Inspiring
November 24, 2022

If my buttons have different names. For example, not (button_1, button_2, button_3...), but (changing the color, creating a picture, combining data). What to do in this case? Underscores(_) are used instead of spaces.

kglad
Community Expert
Community Expert
November 24, 2022

then use:

 

var textA:Array = ["hello","how are you doing","where do you work","goodbye"];

for(var i:int=0;i<textA.length;i++){

this["button_"+(i+1)].addEventListener(MouseEvent.MOUSE_OVER, overF);

this["button_"+(i+1)].addEventListener(MouseEvent.MOUSE_OUT, outF);

}

function overF(e:MouseEvent):void{

Textfield.text=textA[Number(e.currentTarget.name.split("_")[1])];

}

 

function outF(e: MouseEvent): void {
Text field.text = "";
}

kglad
Community Expert
Community Expert
November 23, 2022

// just edit textA to add those 50 strings and the code will still work without changing anything else.

 

 

var textA:Array = ["hello","how are you doing","where do you work","goodbye"];

for(var i:int=0;i<textA.length;i++){

this["button_"+(i+1)].addEventListener(MouseEvent.MOUSE_OVER, overF);

this["button_"+(i+1)].addEventListener(MouseEvent.MOUSE_OUT, outF);

this["button_"+(i+1)].ivar = i;

}

function overF(e:MouseEvent):void{

Textfield.text=textA[e.currentTarget.ivar];

}

 

function outF(e: MouseEvent): void {
Text field.text = "";
}

 

Colin Holgate
Inspiring
November 23, 2022

Your method used the same technnique he was using, but in less lines. Hopefully you will like my approach!

kglad
Community Expert
Community Expert
November 23, 2022

@Colin Holgate 

 

i think you need some problem checking in that text function because we know there's more than those buttons on-stage (eg, the textfield) and that will trigger an error unless you add a few more lines.