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

GameMaker Audio Error When Object is Destroyed (earr@pe sound)

R

Rahiokawines

Guest
Once all enemies in the room are dead, my gate is destroyed. I set it so when the gate is destroyed, a sound effect plays. It works fine when in sounds menu, but in game, it is super loud and keeps repeating itself untill i exit the room. I have tried using different sounds and it still happens so I think its in the coding. here it is:

oPlayer Step Event

//Next Level Gate break
if(instance_number(oEnemy) <= 00)
{
instance_destroy(oGate);
object_set_visible(oGate,false); // I could Still see Gate after "destroy" so I added this.
audio_play_sound(sdGateOpen, 10, false)
}
 

Yal

šŸ§ *penguin noises*
GMC Elder
You have it in the step event, so it will play 60 times per second... use a variable to stop playing it once you've played it once.
 
C

CedSharp

Guest
First of all, instance_destroy() doesn't take any arguments. It will destroy the object that is calling it.
To destroy another object, you can use the "with()" statement, that allows you to execute a script as if it was running inside that object.

In this case, the proper usage of "instance_destroy()" to destroy the gate would be:
Code:
with(oGate) {
  // This code will run inside the oGate object,
  // like if we were executing something in
  // the step event
  instance_destroy()
}
Then, you can either create a variable to track if you played the audio or not, like @Yal mentioned,
or you could make a very simple addition to your if() condition:
Code:
if(instance_number(oEnemy) == 0 && instance_exists(oGate)) {
  with(oGate) instance_destroy(); // This line makes the above if condition to never be true again
  audio_play_sound(sdGateOpen, 10, false);
  // Note you don't need the visibility anymore
  // since we actually destroyed the gate this time
}
Hope this helps
 
R

Rahiokawines

Guest
First of all, instance_destroy() doesn't take any arguments. It will destroy the object that is calling it.
To destroy another object, you can use the "with()" statement, that allows you to execute a script as if it was running inside that object.

In this case, the proper usage of "instance_destroy()" to destroy the gate would be:
Code:
with(oGate) {
  // This code will run inside the oGate object,
  // like if we were executing something in
  // the step event
  instance_destroy()
}
Then, you can either create a variable to track if you played the audio or not, like @Yal mentioned,
or you could make a very simple addition to your if() condition:
Code:
if(instance_number(oEnemy) == 0 && instance_exists(oGate)) {
  with(oGate) instance_destroy(); // This line makes the above if condition to never be true again
  audio_play_sound(sdGateOpen, 10, false);
  // Note you don't need the visibility anymore
  // since we actually destroyed the gate this time
}
Hope this helps
The oGate code is in the create event?
 
C

CedSharp

Guest
The oGate code is in the create event?
The code is simply yours, but modified. You mentioned your code was in the step event of oPlayer ?
That's where the code goes.

The first code of my post was to show you how to use the "with()" statement, but as @TsukaYuriko mentioned,
you don't even have to use my method since I was wrong and in GMS 2, instance_destroy() does take an ar gument, the target instance to destroy.
 
Top