• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Code Not Working...Please Help

W

WillCampbell21

Guest
Basically, I am creating a soundboard and I am trying to make it so that when the user presses the button, the sound plays and the instance changes to another object (purple button to pink button) this makes it so that the user cannot continuously press the button and they know that the sound is playing.

Here is my code:
if audio_is_playing(snd_druggo)
{
instance_change(obj_💩💩💩💩indruggo2, true);
}
else
{
audio_play_sound(snd_druggo, 1, 0);
instance_change(obj_💩💩💩💩indruggo, true);
}
Thanks Guys!
 

Attachments

sp202

Member
Never use instance_change, it is far safer to use instance_create followed by instance_destroy. But in this case I think you can just change the sprite. Am I correct in assuming you want the player to be unable to press the same button if the sound is still playing?
 
W

WillCampbell21

Guest
Yes. I want it so that when the player presses the button, the sound plays and then it stops them from pressing it again until the sound has finished.
 

Ladi_Pix3l

Member
It might be easier to have the a button in one object and control the color change based on a variable.

create
Code:
inUse = false;
canPress = false;
alarm[0] = 5;
left mouse pressed
Code:
if (!inUse) && (canPress)
{
    inUse = true;
    audio_play_sound(snd_druggo,1,false);
    canPress = false;
    alarm[0] = 5;
}

if (inUse) && (canPress)
{
    inUse = false;
    audio_stop_sound(snd_druggo);
    canPress = false;
    alarm[0] = 5;
}
alarm 0
Code:
canPress = true;
draw
Code:
//Assuming you've separated each image

if inUse == true
draw_sprite(sprButtonOn,0,x,y);
else
draw_sprite(sprButtonOff,0,x,y);
Probably not the best way but it's less buggy than instance_change
 
W

WillCampbell21

Guest
That code works better than any i've tried so far thank you! Now when I press it the sound plays and the sprite changes and when I press it again it stops. Any ideas on how to get it to change back automatically as the sound ends?
 

sp202

Member
For something like a soundboard what I recommend you do is instead of having an object for each sound, create one generic button object which has attributes you can assign values to.
Code:
//CREATE
button_spr=0
button_snd=0
pressed=false

//LEFT PRESSED
if !pressed
{
audio_play_sound(button_snd, 1, 0);
alarm[0]=room_speed*audio_sound_length(button_snd)
pressed=true
}

//ALARM
pressed=false

//DRAW
draw_sprite(button_spr,pressed,xy) // Assuming the button pressed and not pressed images are index 1 and 0 respectively of the one sprite
Now you can just do something like
Code:
inst=instance_create(objButton,x,y)
inst.button_spr=sprSound1
inst.button_snd=sndSound1
 
Last edited:
W

WillCampbell21

Guest
Code:
inst=instance_create(objButton,x,y)
inst.button_spr=sprSound1
inst.button_snd=sndSound1
For this part, where would I place this code?
 

Ladi_Pix3l

Member
That code works better than any i've tried so far thank you! Now when I press it the sound plays and the sprite changes and when I press it again it stops. Any ideas on how to get it to change back automatically as the sound ends?
I guess you can use the step event to check. Please bare with me as I have never done this

step
Code:
if (inUse) && (!audio_is_playing(snd_druggo);
{
    inUse = false;
}
You might need to make a buffer first though so use this instead and tell me if this works
add buff1 = false to your create event

and then your step should look like this...
Code:
if (buff1)
{
    if (inUse) && (!audio_is_playing(snd_druggo);
    {
        inUse = false;
        buff1 = false;
    }
}
then add buff1 = true; to your alarm 0
 

sp202

Member
@WillCampbell21 You'd have a control object which will spawn in all the soundboard buttons. So you wouldn't place the buttons in the room editor but rather spawn them in using the control object's Create event. If you'd rather place the buttons directly in the room editor, you would put this snippet of code inside each instance's room creation code:
Code:
button_spr=sprSound1
button_snd=sndSound1
 
W

WillCampbell21

Guest
Code:
if (buff1)
{
    if (inUse) && (!audio_is_playing(snd_druggo));
    {
        inUse = false;
    }
}

In Object obj_💩💩💩💩indruggo, in Event StepNormalEvent action number 1 at line 3 : malformed if statement
Compile Failed - Please check the Compile window for any additional information
Unfortunately it will not compile.
 
W

WillCampbell21

Guest
Thank you guys! I've got it working exactly how I want now!
 
Top