• 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!

Legacy GM Play sound when sprite index changes

J

JakobLangedal

Guest
How can i play a sound once when a sprite index changes? My current code looks like this, but the sound just continues to loop:
if sprite_index = spr_x;
{audio_play_sound (sound_buzzer,1,false);}
 

Llama_Code

Member
Where do you have this code, and is spr_x the sprite you are changing to?

If you have that in a step even or something its going to loop as long as sprite_index = spr_x because that is the only condition you are checking for.
 
J

JakobLangedal

Guest
I have the code in my step event. And yes, spr_x is the sprite im changing to. Where should i put it?
 
J

JakobLangedal

Guest
Where do you have this code, and is spr_x the sprite you are changing to?

If you have that in a step even or something its going to loop as long as sprite_index = spr_x because that is the only condition you are checking for.
I have the code in my step event. And yes, spr_x is the sprite im changing to. Where should i put it?
 

jo-thijs

Member
Hi and welcome to the GMC!

First of all, that's not the code you're using, as it wouldn't compile.
You have a semicolon too much on your first line for that.

Secondly, using code that looks like that, you would have many sounds play over each other,
rather than have 1 sound loop.

Can you please code paste the exact code you're using?
 
J

JakobLangedal

Guest
Hi and welcome to the GMC!

First of all, that's not the code you're using, as it wouldn't compile.
You have a semicolon too much on your first line for that.

Secondly, using code that looks like that, you would have many sounds play over each other,
rather than have 1 sound loop.

Can you please code paste the exact code you're using?
/// Change the sprite

if keyboard_check (vk_numpad1){
sprite_index = spr_x;}

if keyboard_check (vk_numpad4){
sprite_index = spr_x;}

if sprite_index = spr_x;
{audio_play_sound (sound_buzzer,1,false);}

And the code is written in a step event.
 

Tthecreator

Your Creator!
If i just look at your question, and not your sample code I whould say you could set a variable called last_sprite_index to the current sprite_index Inside your create event.
You create event whould look like this:
Code:
last_sprite_index=sprite_index
Now inside the step event we can check the if the current sprite index has changes from the one we stored inside the create event. When we do this we will have to "reset" the last_sprite_index variable again else the sounds will play over each other.
Your step event would look like:
Code:
//the !() means that we want to execute this code when our statement is NOT true.
if !(last_sprite_index=sprite_index) then{
audio_play_sound (sound_buzzer,1,false);
last_sprite_index=sprite_index;//reset variable
}
Then if i look at the code you just send, i'd say why not just put audio_play_sound inside the brackets{} of where you are doing your keyboard_checks?
Like this:
Code:
if keyboard_check (vk_numpad1){
sprite_index = spr_x;
audio_play_sound (sound_buzzer,1,false);}

if keyboard_check (vk_numpad4){
sprite_index = spr_x;
audio_play_sound (sound_buzzer,1,false);}
If you want one "central" function, you could easily change, you could create a new script, or use a variable for that like this:

Code:
var playsound=false;

if keyboard_check (vk_numpad1){
sprite_index = spr_x;
playsound=true;
}

if keyboard_check (vk_numpad4){
sprite_index = spr_x;
playsound=true;
}

if playsound=true then{
audio_play_sound (sound_buzzer,1,false);}
 

jo-thijs

Member
@JakobLangedal,
And you can actually run that code?
When I try that, it doesn't compile.

@Tthecreator,
Those are indeed the 2 ways to go, except that:
1) you should use (a != b) instead of !(a = b) (for readability, people have made mistakes because of this already)
2) keyboard_check would still fire audio_play_sound every step, he should use keyboard_check_pressed in that case,
but I lack code to be able to tell if it could break something else.
 
J

JakobLangedal

Guest
If i just look at your question, and not your sample code I whould say you could set a variable called last_sprite_index to the current sprite_index Inside your create event.
You create event whould look like this:
Code:
last_sprite_index=sprite_index
Now inside the step event we can check the if the current sprite index has changes from the one we stored inside the create event. When we do this we will have to "reset" the last_sprite_index variable again else the sounds will play over each other.
Your step event would look like:
Code:
//the !() means that we want to execute this code when our statement is NOT true.
if !(last_sprite_index=sprite_index) then{
audio_play_sound (sound_buzzer,1,false);
last_sprite_index=sprite_index;//reset variable
}
Then if i look at the code you just send, i'd say why not just put audio_play_sound inside the brackets{} of where you are doing your keyboard_checks?
Like this:
Code:
if keyboard_check (vk_numpad1){
sprite_index = spr_x;
audio_play_sound (sound_buzzer,1,false);}

if keyboard_check (vk_numpad4){
sprite_index = spr_x;
audio_play_sound (sound_buzzer,1,false);}
If you want one "central" function, you could easily change, you could create a new script, or use a variable for that like this:

Code:
var playsound=false;

if keyboard_check (vk_numpad1){
sprite_index = spr_x;
playsound=true;
}

if keyboard_check (vk_numpad4){
sprite_index = spr_x;
playsound=true;
}

if playsound=true then{
audio_play_sound (sound_buzzer,1,false);}
I tried to put the audio in bracelets, just like you suggested, but i have more than 1 object that makes the sound, and the audio somehow sounded really weird, like it was playing more than 1 sound when i pressed the button. I have no idea why, but i thought id try to get the sound to play in another way. Do you have any suggestions on how to fix it? I only want to play one sound at the time.
 

Tthecreator

Your Creator!
@JakobLangedal,
And you can actually run that code?
When I try that, it doesn't compile.

@Tthecreator,
Those are indeed the 2 ways to go, except that:
1) you should use (a != b) instead of !(a = b) (for readability, people have made mistakes because of this already)
2) keyboard_check would still fire audio_play_sound every step, he should use keyboard_check_pressed in that case,
but I lack code to be able to tell if it could break something else.
I tried to put the audio in bracelets, just like you suggested, but i have more than 1 object that makes the sound, and the audio somehow sounded really weird, like it was playing more than 1 sound when i pressed the button. I have no idea why, but i thought id try to get the sound to play in another way. Do you have any suggestions on how to fix it? I only want to play one sound at the time.


@jo-thijs, yes I'm stupid, that was a simple mistake I should have seen, but whatever these things happen, especially since I'm just editing some other code, and not just testing my own.

Anyways, just do as thijs suggested, and change keyboard_check to keyboard_check_pressed.

About the multiple instances problem, you could use some global variables trick and some times, but the most easiest way would be using "audio_is_playing(sound_buzzer)"
Just use this code whenever you want to call your play_sound function:
Code:
if !audio_is_playing(sound_buzzer) then{
audio_play_sound (sound_buzzer,1,false)
}
Code is untested though and I only looked quickly in the manual, but it should work.
 
Top