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);
// 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
...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
...
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 = "";
}
Copy link to clipboard
Copied
Your method used the same technnique he was using, but in less lines. Hopefully you will like my approach!
Copy link to clipboard
Copied
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.
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.
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())
Copy link to clipboard
Copied
If you can put your FLA online somewhere I can check what you are doing differently.
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 = "";
}
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.
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 = "";
}