[SOLVED]Making Button

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

Previous topic - Next topic

VegeTato

Hi,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.

dayowlron

Set the buttons NormalGraphic property.

btnButton.NormalGraphic = 15;

where 15 is the sprite with the color of the button you want.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

VegeTato

i don't understand"Set the buttons NormalGraphic property."
i don't have a buttons.... this is my question how to create a buttons then animate them >.<

can you explain more ? :3

Cassiebsg

Create a GUI, add a button to it...
Or create an object in the room and give it the "start game" sprite number, then change it to the green sprite number...

Or give us more info about what you have done so far and how you want your button to be.
There are those who believe that life here began out there...

VegeTato

Quote from: Cassiebsg on Sat 08/04/2017 21:55:32
Create a GUI, add a button to it...
Or create an object in the room and give it the "start game" sprite number, then change it to the green sprite number...

Or give us more info about what you have done so far and how you want your button to be.

i have created an object in my game and i give it the "start game" sprite and it works fine,but i don't know how to animate it
because there is only (talk to object,interact,etc...) there is no option for "walk over object" or aim on the object.
so when i aim(my mouse) at the "start game" it turns to another sprite(green,start game)
any way to do that ? :/

a screen shoot for what i have done:
   

Snarky

In the AGS Editor, press the F1 button on your keyboard. The help file will pop up. It includes a tutorial with explanations of how to perform basic AGS tasks. Read it. The relevant sections here are under "Tutorial | Setting up the game | Animations" and "Other Features | Editing the GUIs", but you should have a look at the whole tutorial.

Slasher

#6
As per Snarky

In the objects events property panel click interact and a function will appear in the room script.

Just add something like this as an example:

Code: ags

function StartGame_Interact() // AGS Adds this Interact function
{
player.Walk(oStartGame.X,oStartGame.Y+10,eBlock,eWalkableAreas); // player walks to start game object
player.Loop=3; // face object

// Animate object this like:
oStartGame.SetView(10) // or whatever view it is you have made
oStartGame.Animate(0,4,eOnce,eBlock); // will play loop 0 only once

// Do this like start the game etc
{


At least it's a start


VegeTato

Quote from: slasher on Sun 09/04/2017 16:33:21
As per Snarky

In the objects events property panel click interact and a function will appear in the room script.

Just add something like this as an example:

Code: ags

function StartGame_Interact() // AGS Adds this Interact function
{
player.Walk(oStartGame.X,oStartGame.Y+10,eBlock,eWalkableAreas); // player walks to start game object
player.Loop=3; // face object

// Animate object this like:
oStartGame.SetView(10) // or whatever view it is you have made
oStartGame.Animate(0,4,eOnce,eBlock); // will play loop 0 only once

// Do this like start the game etc
{


At least it's a start

Thanks :D now when i press on Start game it turns to green :)
but i didn't know how to animate when i aim on the button

Example:
When i move my pointer on the button or object i want it to change to another color
When i aim it turns to yellow when i out the area it goes back to red.

is that possible to make ? :) (Aiming on button animate) Thanks.

Slasher

Right...

Are we talking gui control Button or an Object?

VegeTato

Quote from: slasher on Mon 10/04/2017 03:24:41
Right...

Are we talking gui control Button or an Object?
i am using object, because when i use gui button it keep showing that annoying background, i want my button to be png,
And in the event section there is only "click on button" option, there is no aim on button or mouse move on button Option.



But if you know how to delete the background in gui and how to animate the button in gui And how to position the button in GUI. then please tell me how :D

So i am now using Object(Start_button) and View3(animating Start_button) And character(player) and another character to talk to him(Dead)



This is the code for Menu room i used after you told me how to animate the button(Thanks again :))
Code: ags
function room_Load()
{
aKako.Play();  // play music
}
function Start_button_AnyClick()  // animate button
{
Start_button.SetView(3);
Start_button.Animate(0,25, eOnce);
player.ChangeRoom(2);
}


Cassiebsg

The easiest way is to make a GUI, with a transparent background.
Then create a button, in the button's properties you can then set the sprite numbers for "normal", "when mouse over" and "pushed/clicked"...  ;)
Use the button event to then code what happens when you push/click the button.
There are those who believe that life here began out there...

Slasher

#11
For reference

To set any gui to be transparent in its properties panel open up color and choose black or enter 0 in the color option for the background. The gui area will be magenta colour which is the transparency colour.

Snarky

You don't see the button properties because the properties pane has been set to the "events" tab. Click on the little list button next to the thunderbolt to switch it back to properties.

VegeTato

Thank you so much guys ^^ every thing is working fine now ;-D i have animated the button When aim on it, and animated it when i click :3

Now i got another 2 new problems :-X if they got solved ill be so thankful :cheesy: And i can start my project to the NEW GAME ! :D Please help me :)

Problem 1:
So after i made the button code when click(player change room)to room 2(i was in room 1)the problem is The button keep showing in room2 i thought it will only show in room1 but looks like it comes to all my rooms :-[ Any idea how to fix ?

Problem 2:
is there is any way to add an audio when i aim at the button ?
Because in the events section there is only "OnClick" option.
i want to add an audio when my mouse aim on the button Is that possible ? :-\

(Thanks for your time).

Slasher

#14
Check in help manual for: GetAtScreenXY

Examples in Global Rep Exec
Code: ags

}
  if(GUIControl.GetAtScreenXY(mouse.x, mouse.y) ==Button3){  // If over a gui button
   aKNOCK.Play(); // will play this sound when mouse over
   
}
  if(player.Room==3 && Object.GetAtScreenXY(mouse.x, mouse.y) ==object[5]){ // if over a room object
    aKNOCK.Play(); // will play this sound when mouse over


If the button is an object in room one then it should only show in room one and not two..if they are objects..
objects only relate to the room the are in.



Khris

I don't know how you implemented the button, but if you used a GUI, just set it to invisible before sending the player to the next room.

As for playing a sound: you wouldn't want the sound to run over and over again. Here's how to set that up:

Code: ags
// above repeatedly_execute() in GlobalScript.asc
bool wasOverButton = false;

  // add this *inside* repeatedly_execute()
  bool isOverButton = GUIControl.GetAtScreenXY(mouse.x, mouse.y) == Start_button;
  if (isOverButton && !wasOverButton) btnSound.Play();
  wasOverButton = isOverButton;

dayowlron

If the button is showing in both rooms then it sounds like the button is on a gui. to make it disappear you need to make the gui invisible.

gGui.Visible=false;
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

VegeTato

Problem 1: Solved by adding this code to my room to invisible the button :D Thanks
Code: ags
Start_button.Visible=false;


Problem 2:didn't work :((i am using Gui button and what i want is to hear a sound(aPsh) when i move the mouse over the button)

i tired what you guys said and here is the result:

Quote from: slasher on Tue 11/04/2017 02:44:41
Check in help manual for: GetAtScreenXY

Examples in Global Rep Exec
Code: ags

}
  if(GUIControl.GetAtScreenXY(mouse.x, mouse.y) ==Button3){  // If over a gui button
   aKNOCK.Play(); // will play this sound when mouse over
   
}
  if(player.Room==3 && Object.GetAtScreenXY(mouse.x, mouse.y) ==object[5]){ // if over a room object
    aKNOCK.Play(); // will play this sound when mouse over


If the button is an object in room one then it should only show in room one and not two..if they are objects..
objects only relate to the room the are in.



I have tried that and the result is when i start the game the sound keep playing (and i didn't even aim my mouse at the button)the sound just keep playing again and again in all the rooms. here is the code i used:
Code: ags
function room_Load()
{
aKako.Play();  // play music
if(GUIControl.GetAtScreenXY(318, 157)){  // If over a gui button
aPsh.Play(); // will play this sound when mouse over
}
}

then i tired this code but when i run the game it gives me Error (line 4): undefined symbol 'button3'
Code: ags
function room_Load()
{
aKako.Play();  // play music
if(GUIControl.GetAtScreenXY(318, 157)==button3){  // If over a gui button
aPsh.Play(); // will play this sound when mouse over
}
}


Quote from: Khris on Tue 11/04/2017 10:39:37
I don't know how you implemented the button, but if you used a GUI, just set it to invisible before sending the player to the next room.

As for playing a sound: you wouldn't want the sound to run over and over again. Here's how to set that up:

Code: ags
// above repeatedly_execute() in GlobalScript.asc
bool wasOverButton = false;

  // add this *inside* repeatedly_execute()
  bool isOverButton = GUIControl.GetAtScreenXY(mouse.x, mouse.y) == Start_button;
  if (isOverButton && !wasOverButton) btnSound.Play();
  wasOverButton = isOverButton;

When i tired this code it gives me Error (line 4): Type mismatch: cannot convert 'GUIControl*' to 'GUI*' Here is the code i used:
Code: ags
function room_Load()
{
aKako.Play();  // play music
  bool isOverButton = GUIControl.GetAtScreenXY(314, 157) == Start_button;
  if (isOverButton && !wasOverButton) aPsh.Play();
  wasOverButton = isOverButton;
}

+i don't understand what bool code used for...

Any idea how to fix all this ? :undecided:

Information's about my project:
  • aPsh = The sound i want to play when i aim at the button.
  • I am using GUI button not object.
  • aKako.Play(); is just a code to play background music its working fine.
  • My GUI button name is Start_button

Please help in this final problem :~(

Slasher

Both I and Khris' code works.

Using Khris'

Code: ags

// in Global

bool wasOverButton = false; // Placed above function repeatedly_execute() 

function repeatedly_execute() {   

  bool isOverButton = GUIControl.GetAtScreenXY(mouse.x, mouse.y) == Start_button; // 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;
}


Provided your GUI is not set to 'Pause Game when shown' then the sound will play.

This works...So make sure you add it correctly as shown...

Error (line 4): undefined symbol 'button3' means ags can't find button3 probably because it does not exist. My code only used button3 as an example.




dayowlron

I did notice you are putting ur code in room_Load and the code needs to be in repeatedly_execute.
Do you have some code in repeatedly_execute that is making the sound?
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

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