Play Sound Once with Collision Animation
#1
Posted 22 June 2012 - 02:48 AM
Also, is there a way to place this coding in the controller_object and have it apply to all my different collision animations and sounds instead of repeating each one?
I am a newbie, so I would appreciate a step by step answer. A previous 2007 similar answer was leaving me with error codes that I couldn't figure out how to make work.
#2
Posted 22 June 2012 - 03:01 AM
Well the first part will be:Upon collision, my animation repeats. I want the sound associated with it to play only once.
Also, is there a way to place this coding in the controller_object and have it apply to all my different collision animations and sounds instead of repeating each one?
I am a newbie, so I would appreciate a step by step answer. A previous 2007 similar answer was leaving me with error codes that I couldn't figure out how to make work.
with (object and object2){
}Replace object with the first name of the object example: obj_wall and if you have another object you can use "and" that will make the with statement work also with the other object example: obj_player, if you need more just add "and name_of_the_object" otherwise remove the "and object2" if you don't need it so your code will look like:
with (object){
if (image_index=0 and played=0){
sound_play(something);//I think thats the code to play a sound, don't remember at the moment xD
}//0 is just a reference, you can use 1,2,3, depending of the number of sub-images and check if this event was executed before (you can reset it with played=0)
}
Edited by Debels, 22 June 2012 - 03:02 AM.
#3
Posted 22 June 2012 - 07:02 AM
I've tried placing your code, but program isn't recognizing played and I am getting a second error message.
with (obj_guy){
if (image_index=0 and played=0){
sound_play(snd_noise);
I've also tried the following code, but it has errors.
If sp = false
{
if !sound_isplaying(snd_noise)
sound_play(snd_noise)
sp = true
}
if !place_meeting(x,y,object) and sp=true{
sp=false}
if !sound_isplaying(sound)
sound_play(sound)
I've tried pausing the sound after it plays once, but i don't get any sound when I do. I barely know GL coding, so is there a simple way to use the drag and drop feature for this?
Edited by Le977, 22 June 2012 - 07:04 AM.
#4
Posted 22 June 2012 - 08:39 AM
The animation only has two images, but when it collides it's repeating image 1, image 2, image 1, image 2,image 1,image 2 which I want but my sound is repeating 3 times. I want my sound to play once before the next collision.
I've tried placing your code, but program isn't recognizing played and I am getting a second error message.
with (obj_guy){
if (image_index=0 and played=0){
sound_play(snd_noise);
"played" is a variable. All variables need to be initialized. To initialize a variable, in the "create" event of an object, just simply put the code:
played = 0;
On another note, the code he gave you isn't complete. He is using "played" as a flag, saying that the sound has already been played, however he is not setting the flag. A more complete code would be:
CREATE EVENT CODE:
played = 0;
COLLISION EVENT CODE:
with (obj_guy){
if (image_index==0 and played==0){
sound_play(snd_noise);
played = 1;
}
}Now there is one more problem with the code above, depending on what you're doing. In its current state, the sound will only play once, period. Do you want these objects to ever stop colliding at some point so they can collide and make the sound again? If not, this code will work for you.
#5
Posted 23 June 2012 - 02:29 PM
I placed the following code in the collision event:
played = 0;
with (obj_guy){
if (image_index==0 and played==0){
sound_play(snd_noise);
played = 1;
}
}
My game is basically a scrolling game with repeated collisions every few seconds. The obj_guy is actually colliding about four times during each object collision.
I was wondering if there was a code to create a time delay before the sound can play again. If sound is played,it cannot be played again for 3 seconds.
#6
Posted 23 June 2012 - 04:38 PM
The error message disappeared, but the code is still not working correctly. Now the sound is repeating during the first couple of collisions and silent during the last few before the game resets.
I placed the following code in the collision event:
played = 0;
with (obj_guy){
if (image_index==0 and played==0){
sound_play(snd_noise);
played = 1;
}
}
That is not the code that I gave you. The first line "played = 0;" goes in the CREATE event, not the collision event.
If your object gets deleted after the collision then you won't have to worry about a timer. However there are alarms you can set like "alarm[0] = 10" then create an "Alarm 0" event and put the code there you want to execute after the time runs out, like "played = 0".
#7
Posted 26 June 2012 - 04:38 AM
Originally, I did have the code separated, but I couldn't get it to work so for a few hours I tried different combos. I returned to the code you suggested.
The problem was that I actually had two obj_guys. obj_guy1 collision action into an object with a change instance into obj_guy2 which continued the collision action. The solution was to place the first part of the code in obj_guy2, then create a second collision action for the play music where the second part of the code (referring to obj_guy1) was placed. Whew!
Thanks for all your help. I can finally finish my game.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











