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

Code optimization

Contributor ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

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);

 

 

TOPICS
ActionScript , Code

Views

667

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

correct answers 2 Correct answers

Community Expert , Nov 23, 2022 Nov 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: Mous

...

Votes

Translate

Translate
LEGEND , Nov 23, 2022 Nov 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];
}

func
...

Votes

Translate

Translate
Community Expert ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

// 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 = "";
}

 

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
LEGEND ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

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

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
Community Expert ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

@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.

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
LEGEND ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

I did test for that, and although you're right that there may need to be extra checking, the textfield isn't a problem. Only objects that have an instance name that includes underscore would be a problem. To take care of that the name could be checked to see if it starts with "button_". The if(btnnumber) handles all objects that didn't happen to have an underscore in the name.

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
Contributor ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

Something went wrong. Two errors occur.
First, when I test the video, an error appears on the Output panel: (ReferenceError: Error #1056: Cannot create property ivar on flash.display.SimpleButton.
at Nameless_1_fla::MainTimeline/frame1())

When hovering over the button, another error appears: (ReferenceError: Error #1069: Property bar not found on flash.display.SimpleButton and there is no default value.
at Nameless_1_fla::MainTimeline/overF())

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
LEGEND ,
Nov 24, 2022 Nov 24, 2022

Copy link to clipboard

Copied

LATEST

If you can put your FLA online somewhere I can check what you are doing differently.

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
LEGEND ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

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 = "";
}

 

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
Contributor ,
Nov 23, 2022 Nov 23, 2022

Copy link to clipboard

Copied

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.

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
Community Expert ,
Nov 24, 2022 Nov 24, 2022

Copy link to clipboard

Copied

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 = "";
}

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