Play Sound Effect When Mouse is Over GUI Button...(SOLVED)

Started by J.E.S., Fri 12/09/2008 07:18:18

Previous topic - Next topic

J.E.S.

Hello, I can't seem to figure out how to play a sound once and not loop when you mouseover a gui button. I tried experimenting with the code from this post :http://www.adventuregamestudio.co.uk/yabb/index.php?topic=28715.0 but I still can't get it right. What I'm trying to make is a gui that appears when you press escape to show the main game menu screen and when you mouse over a button, you'll hear a click sound. Thanks in advance.

Dualnames

add this at the top of your global script.
GuiButton*gbtton;
bool played=false;

then at the repeatedly execute
gbtton=GuiButton.GetAtScreen(mouse.x,mouse.y);
if (gMainmenu.Visible==false) {
played=false;
}
if (gMainmenu.Visible==true) {
if (gbtton==null) {
played=false;
}
if (gbtton!=null) {
if (played==false) {
played=true;
PlaySound(11);//whatever number that is
}
}
}

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

J.E.S.

Thanks for the quick response! I feel stupid for asking this, but is there something else I need to do to my code? because I get this error when I run it:

Parse error: unexpected 'Guibutton'

here is my code:
Code: ags
// main global script file
Guibutton*gbtton;
bool played=false;



#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() // called when the game starts, before the first room is loaded
  {
    
  }
#sectionend game_start  // DO NOT EDIT OR REMOVE THIS LINE


#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute()
{
 gbtton=GuiButton.GetAtScreen(mouse.x,mouse.y);
if (gEscMenu.Visible==false) {
played=false;
}
if (gEscMenu.Visible==true) {
if (gbtton==null) {
played=false;
}
if (gbtton!=null) {
if (played==false) {
played=true;
PlaySound(1);
}
}
}


I thought you were trying to say that Guibutton was a placeholder for the real gui button name but that didn't work either.

Matti

In the global header you wrote Guibutton and in the rep-ex you wrote GuiButton. Could that be the problem?

Sorry, forget it.

J.E.S.

@matti...I went back and fixed the capitalization, but I still got the same error.

Matti

..and when you changed "GuiButton" to the actual name of the Button (which is neccessary I think), did you put a "g" before the name?

J.E.S.

No I didn't, the name of the button is "ResumeGame" so that's what I put.

Joe

#7
Ok, there is another possibility:

First of all: you must give the button an SCRIPT name (e.g.:ResumeGame) and I supose the Main menu script name is "gMainmenu".

Code: ags


//at top of global script
bool played=false;


//at repeatedly_execute

if(played==false){
if((mouse.x >ResumeGame.X+gMainmenu.X)&&(mouse.x <ResumeGame.X+ResumeGame.Width+gMainmenu.X)){
if((mouse.y >ResumeGame.Y+gMainmenu.Y)&&(mouse.y <ResumeGame.Y+ResumeGame.Height+gMainmenu.Y)){

PlaySound(1);
played=true;

}
}
}

if(played==true){
if((mouse.x <ResumeGame.X+gMainmenu.X)||(mouse.x >ResumeGame.X+ResumeGame.Width+gMainmenu.X)){
if((mouse.y <ResumeGame.Y+gMainmenu.Y)||(mouse.y >ResumeGame.Y+ResumeGame.Height+gMainmenu.Y)){

played=false;

}
}
}


Copinstar © Oficial Site

J.E.S.

@Joe Carl...I was able to get your code to work and I thank you for helping me. What I'm trying to do now is apply this functionality to the rest of the buttons on my gui(save, load, quit....etc). Is there a way I can do this with minimal code or will I have to cut and paste this same code for each button?

paolo

Quote from: J.E.S. on Sat 13/09/2008 00:47:53
@Joe Carl...I was able to get your code to work and I thank you for helping me. What I'm trying to do now is apply this functionality to the rest of the buttons on my gui(save, load, quit....etc). Is there a way I can do this with minimal code or will I have to cut and paste this same code for each button?

For more than one button, you'll need more than one "played" variable and checks for each of the buttons:

Change the code at the top of the global script to this:
Code: ags

bool playedResumeGameSound = false;
bool playedSaveSound = false;
bool playedLoadSound = false;

and so on, for each button.

Then in repeatedly_execute, change every instance of "played" to playedResumeGameSound. Then add exactly the same code underneath for playedLoadSound, etc, changing playedResumeGameSound to playedLoadSound, and so on for each of your buttons. That should do the trick.

It's probably possible to factor this code out into a function of its own to cut down the amount of copying and pasting you'll need to do, but I'll leave that to someone else to do.

J.E.S.

I'll mark this topic as solved. Here is a different version that works also, and uses less code...

Code: ags

// main global script file
bool playedResume = false;
bool playedSave = false;

...

function repeatedly_execute()

{
//------------------------------------Resume Game----------------------------------------------  
if(playedResume==false)
{
if(ResumeGame.Graphic==159)
  PlaySound(1);
  playedResume=true;
}

if(playedResume==true)
{
if(ResumeGame.Graphic==158)
playedResume=false;
}

//-------------------------------------Save Game-------------------------------------------------

 if(playedSave==false)
{
if(Save.Graphic==148)
  PlaySound(1);
  playedSave=true;
}

if(playedSave==true)
{
if(Save.Graphic==147)
playedSave=false;
} 


}    





I want to thank everyone who replied to this topic and pointed me in the right direction.

SMF spam blocked by CleanTalk