Adding Sound to a Button (SOLVED)

Started by tierdal, Thu 16/05/2013 06:22:30

Previous topic - Next topic

tierdal

Hi all! While making a main menu I came up to a small problem. Everything is fine BUT I cannot figure out how to Assign sound effects (like a little beep) to a Button within the GUI. I want it to be so where when I mouse over an Item on the menu, that sound is played. If you need more information, please let me know.

Khris

#1
You need a state change loop, like this:

Code: ags
// this line above repeatedly_execute_always
Button *old_button;

  // inside repeatedly_execute_always
  GUIControl *gc = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
  Button *b;
  if (gc != null) b = gc.AsButton;

  if (b != old_button && b != null) { // mouse has just entered a button
    if (b.OwningGUI == gMenu) {
      // code here, e.g. aButtonSound.Play();
    }
  }
  old_button = b;


Edit: corrected code

tierdal

Thank you so much, however I have 2 new questions:

1. Is it possible to add sound to just one of the buttons or just to a certain GUI?

2. (I dont think there is a fix for this) but my menu buttons are stacked up in a column, and when I move mouse over them only 1 or 2 sounds play for the whole menu of 6 buttons, to hear the sound again i have to move mouse left or right from the GUI itself..

Khris

Both are easily possible, and my code was faulty anyway.
It should work now even if the mouse goes directly from button to button. I also added a check for the GUI; just replace "gMenu" with your menu GUI's name.

tierdal

kudos to you, good sir. works perfectly!

tierdal

Coming back to the topic of Buttons...

I have this code and I'm looking for a way to simplify it but nothing comes to mind:
(what it does is it changes the font color to black as mouse moves over it and back to white when mouse moves away from it)

Code: AGS
/*=======================  Menu BUTTON SOUNDS & ANIMATION & COLOR  ============================ */  

  GUIControl *gc = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
  Button *b;
  int IsOverButton = false;
  
  if (gc != null) b = gc.AsButton;
 
  if (b != old_button && b != null) { // mouse has just entered a button
    if (b.OwningGUI == gMainMenu) {
      //IsOverButton = true;                                                  //BUTTON SOUND
      aButton_16.Play();
    }  
  }
  
//Button Font Color Changer
  if (b == Main_NewGame) {
      Main_NewGame.TextColor = 0;
       Main_LoadGame.TextColor = 65535;
       Main_Options.TextColor = 65535;
       Main_Credits.TextColor = 65535;
       Main_Extras.TextColor = 65535;
       Main_Help.TextColor = 65535;
       Main_Exit.TextColor = 65535;
     } else if (b == Main_LoadGame) {
       Main_LoadGame.TextColor = 0;
       Main_NewGame.TextColor = 65535;
       Main_Options.TextColor = 65535;
       Main_Credits.TextColor = 65535;
       Main_Extras.TextColor = 65535;
       Main_Help.TextColor = 65535;
       Main_Exit.TextColor = 65535;
     } else if (b == Main_Options) {
       Main_Options.TextColor = 0;
       Main_NewGame.TextColor = 65535;
       Main_LoadGame.TextColor = 65535;
       Main_Credits.TextColor = 65535;
       Main_Extras.TextColor = 65535;
       Main_Help.TextColor = 65535;
       Main_Exit.TextColor = 65535;
     } else if (b == Main_Credits) {
       Main_Credits.TextColor = 0;
       Main_NewGame.TextColor = 65535;
       Main_LoadGame.TextColor = 65535;
       Main_Options.TextColor = 65535;
       Main_Extras.TextColor = 65535;
       Main_Help.TextColor = 65535;
       Main_Exit.TextColor = 65535;
     } else if (b == Main_Extras) {
       Main_Extras.TextColor = 0;
       Main_NewGame.TextColor = 65535;
       Main_LoadGame.TextColor = 65535;
       Main_Options.TextColor = 65535;
       Main_Credits.TextColor = 65535;
       Main_Help.TextColor = 65535;
       Main_Exit.TextColor = 65535;
     } else if (b == Main_Help) {
        Main_Help.TextColor = 0;
       Main_NewGame.TextColor = 65535;
       Main_LoadGame.TextColor = 65535;
       Main_Options.TextColor = 65535;
       Main_Credits.TextColor = 65535;
       Main_Extras.TextColor = 65535;
       Main_Exit.TextColor = 65535;
     } else if (b == Main_Exit) {
       Main_Exit.TextColor = 0;
       Main_NewGame.TextColor = 65535;
       Main_LoadGame.TextColor = 65535;
       Main_Options.TextColor = 65535;
       Main_Credits.TextColor = 65535;
       Main_Extras.TextColor = 65535;
       Main_Help.TextColor = 65535;
     } else {
       Main_NewGame.TextColor = 65535;
       Main_LoadGame.TextColor = 65535;
       Main_Options.TextColor = 65535;
       Main_Credits.TextColor = 65535;
       Main_Extras.TextColor = 65535;
       Main_Help.TextColor = 65535;
       Main_Exit.TextColor = 65535;  
     }
    
  old_button = b;
  //END BUTTON SOUNDS


EDIT: i tried doing a while loop and it crashed :(

Khris

What you do is loop through all the controls, set them to white, then set b's text to black.

Code: ags
  int i;
  GUIButton gb;
  while (i < gMainMenu.ControlCount) {
    gb = gMainMenu.Controls[i].Asbutton;
    if (gb != null) gb.TextColor = 15;  // white
    i++;
  }
  if (b != null) b.TextColor = 0;


But there's of course another possibility:
Code: ags
  if (b != old_button && b != null) { // mouse has just entered button b
    if (b.OwningGUI == gMainMenu) {
      aButton_16.Play();  //BUTTON SOUND
      b.TextColor = 0;
    }  
  }

  if (b != old_button && old_button != null) { // mouse has just left the button old_button
    if (old_button.OwningGUI == gMainMenu) {
      old_button.TextColor = 15;  // white
    }  
  }


The crash you got was probably a null pointer error, right?
You get those whenever you use aaaaa.bbbbb in your code and aaaaa happens to be null.

tierdal


SMF spam blocked by CleanTalk