[SOLVED]Making Button

Started by VegeTato, Sat 08/04/2017 16:17:01

Previous topic - Next topic

Khris

If you need the sound code only in a single room, you can implement the code in your room's repeatedly_execute instead of the global one. Add the event-link and function as usual via the room's thunderbolt icon, and AGS will add this to your room script:
Code: ags
function room_RepExec()
{

}


Now add my code above and inside the function until you have this:
Code: ags
bool wasOverButton = false;
     
function room_RepExec()
{
  bool isOverButton = GUIControl.GetAtScreenXY(mouse.x, mouse.y) == Start_button;
  if (isOverButton && !wasOverButton) aPsh.Play();
  wasOverButton = isOverButton;
}


I assumed that your button is called "Start_button", since in a previous snippet you're calling Start_button.Animate(...). If that's the name of your GUI though, replace GUIControl with GUI in my code and it should work.
Note that while you clearly stated that Start_button is your GUIButton, the error you got says it's a GUI.

Slasher

QuoteHi,i want to make a game like "who want to be a millionaire" so the game is all about buttons.
My question is how to make a button in AGS ? and when i aim at that button it turns to green(for example)
i got the 2 pictures one is (Start game"red color") and i got another one (Start game"green color)
so how to create a button and when i aim on it how to change it to another picture(color). Thanks.
For someone who knows nothing about buttons this could be a long journey if he wants to complete this type of game but I wish him the best of luck...


VegeTato

First i have notice that my button is called Button1 sorry... :( Start_button is the GUI background name.
So i have tried both codes you gave me guys, one in global and one in the room event and both of them got the same problem:
When i start the game the sound Psh start and keep restarting again and again, i didn't aim my mouse i didn't even move it...
Here is the code i used and i have changed Start_button to Button1  as i said before Button1 is my button and Start_button is the GUI Here is the result:
In Global script:
Code: ags
bool wasOverButton = false;
function repeatedly_execute() {
  // Put here anything you want to happen every game cycle, even when
  // the game is paused. This will not run when the game is blocked
  // inside a command like a blocking Walk()
    bool isOverButton = GUIControl.GetAtScreenXY(155, 129) == Button1; // when the mouse is over Start_button
  if (isOverButton && !wasOverButton) aPsh.Play();  //   aPsh sound will play once each time whilst over Start_button
  wasOverButton = isOverButton;
  if (IsGamePaused() == 1) return;
  // Put here anything you want to happen every game cycle, but not
  // when the game is paused.
}


In room Event:
Code: ags
function room_Load()
{
aKako.Play();  // play music
}
bool wasOverButton = false;
function room_RepExec()
{
  bool isOverButton = GUIControl.GetAtScreenXY(155, 129) == Button1;
  if (isOverButton && !wasOverButton) aPsh.Play();
  wasOverButton = isOverButton;
}

i don't understand whats the problem... i have followed all the instructions the only thing that i have changed is
GetAtScreenXY(mouse.x, mouse.y) == Start_button; TO GUIControl.GetAtScreenXY(155, 129) == Button1;

Please help in this confusing problem >.<
  • If you know whats the problem in the codes ? then please tell me :)
  • Here is the game i am working on (this is an example game before i start with who wants to be a millionaire)Take a look and please tell me the code to fix :(
Download link for my project:
http://www101.zippyshare.com/v/WizzU5iK/file.html

Snarky

You're hardcoding the coordinates of the GUIControl.GetAtScreenXY() call instead of using mouse.x and mouse.y like Khris showed you. So instead of checking whether the mouse is over the button, you're checking whether the button is where it's supposed to be, which will always be yes.

VegeTato

Quote from: Snarky on Wed 12/04/2017 00:12:27
You're hardcoding the coordinates of the GUIControl.GetAtScreenXY() call instead of using mouse.x and mouse.y like Khris showed you. So instead of checking whether the mouse is over the button, you're checking whether the button is where it's supposed to be, which will always be yes.
oh i thought i should change it to x and y position of the button... anyway i fix it and now the code is this
Code: ags
function room_Load()
{
aKako.Play();  // play music
}
bool wasOverButton = false;
function room_RepExec()
{
  bool isOverButton = GUIControl.GetAtScreenXY(mouse.x, mouse.y) == Button1;
  if (isOverButton && !wasOverButton) aPsh.Play();
  wasOverButton = isOverButton;
}


now when i run the game my background music (aKako)start normal, and when i aim at the button (Psh) sound starts but when it starts it stop my music(aKako) and it keep restarting...
for example: the sound is (Ding) so when i aim my mouse on the button my music stops and i hear (ding ding ding ding ding...)forever :/ even if i move my mouse outside the button area the Ding sound keep going...
Any idea how to fix:

  • Start sound once only.
  • background music(aKako) don't stop when i aim on the button.
  • Make the sound stop when i get out the button area

Slasher

Just to ask... is aPsh actually in the sound branch.. sounds like you could have added it to the music branch of the audio tree?

Khris

The very first step is to remove all that code from your room_Load. It was never supposed to be there in the first place. I told you specifically to place it in repeatedly_execute / room_RepExec.

As for inserting hardcoded coordinates for my mouse.x, mouse.y, ALWAYS try to run other people's code as-is first, unless told otherwise. When you get an error like

"AGS can't find sprite INSERT_YOUR_SPRITE_SLOT_HERE", then you get to actually make your own changes to it.

VegeTato

Quote from: slasher on Wed 12/04/2017 04:43:30
Just to ask... is aPsh actually in the sound branch.. sounds like you could have added it to the music branch of the audio tree?


how simple was that >.< my sound was in the music folder i move it to sound folder now everything is fine when i aim i hear the sound once only every time i aim,
But the music stops...any idea how to make the music background(aKako) never stop when another sounds play ?  Thanks :)

(The Final Problem)

Slasher

Exactly as i mentioned. Button sound is Music NOT sound....

Slasher

#29
aKako only stops when you click on the button and go to the next room as you have scripted aKako to Stop in room 2..

When a sound is played it should not stop the background music...

Also the gIconbar should be visible=false when in the first room.

Also gui z orders need setting (z order = order of which gui is on top / at back)

'Regions' are generally used for this type of command
Code: ags
function hHotspot1_WalkOn()
{
player.ChangeRoom(3, 246, 420);
}


If you take the key then the key on the table should be an object that disappears when you take it as it gets added to your inventory.


VegeTato

Quote from: slasher on Wed 12/04/2017 14:58:46
aKako only stops when you click on the button and go to the next room as you have scripted aKako to Stop in room 2..

When a sound is played it should not stop the background music...

Also the gIconbar should be visible=false when in the first room.

Also gui z orders need setting (z order = order of which gui is on top / at back)

'Regions' are generally used for this type of command
Code: ags
function hHotspot1_WalkOn()
{
player.ChangeRoom(3, 246, 420);
}


If you take the key then the key on the table should be an object that disappears when you take it as it gets added to your inventory.
Thanks i will edit all this in my project :) but you forgot to tell me how to fix my final problem >.>
Quoteany idea how to make the music background(aKako) never stop when another sounds play ?

This is the final code i used in the Menu room:
Code: ags
function room_Load()
{
aKako.Play();  // play music
}
bool wasOverButton = false;
function room_RepExec()
{
  bool isOverButton = GUIControl.GetAtScreenXY(mouse.x, mouse.y) == Button1;
  if (isOverButton && !wasOverButton) aPsh.Play();
  wasOverButton = isOverButton;
}

i don't understand why the musics stops when the sound Psh starts... :~(

Slasher

#31
When you hover over the button the sound plays but the music does not stop. Move the move away from the button and the music still plays. It's only when you click on the button does the music stop and that is because you told it to stop in Room 2.

In your Room 2

Code: ags

function room_Load()
{
Start_button.Visible=false;
gIconbar.Visible=true;
aKako.Stop(); // Background music stops
}



Snarky

If what slasher is saying does not match your game, there must be some setting that is wrong. Check the AudioType of your clips. Make sure that:

-aKako.AudioType is Music
-aPsh.AudioType is Sound (or AmbientSound)

If that's already true, something else is wrong. In that case, go have a look at each AudioType (Audio | Types in the project tree, double-click on one to open). Check what MaxChannels is set to for each one. You probably ought to have:

-Music.MaxChannels is 1
-Sound.MaxChannels is 0

If that also looks OK, come back.

VegeTato

Quote from: Snarky on Wed 12/04/2017 17:45:13
If what slasher is saying does not match your game, there must be some setting that is wrong. Check the AudioType of your clips. Make sure that:

-aKako.AudioType is Music
-aPsh.AudioType is Sound (or AmbientSound)

If that's already true, something else is wrong. In that case, go have a look at each AudioType (Audio | Types in the project tree, double-click on one to open). Check what MaxChannels is set to for each one. You probably ought to have:

-Music.MaxChannels is 1
-Sound.MaxChannels is 0

If that also looks OK, come back.
YEAAAAAAAAAAAS finally (laugh) my problem solved ^^ the problem was my aPsh was on type:Music now i made it on type:sound Thank you Snarky and slasher and Khris and all who helped me Thanks a lot ;-D
Now i can start my NEW GAME ! (laugh)
SOLVED

Slasher

Glad you have it finally solved (nod)

SMF spam blocked by CleanTalk