Page 1 of 1
How to play sound just once?
Posted:
Wed Apr 25, 2012 4:44 pm
by lverona
Hey guys!
I am attempting to play a sound effects once (not looped) when a player lands on any platform. Nothing I tried seems to work, the sound just keeps on playing looped. Is it possible to play a sound just once?
Re: How to play sound just once?
Posted:
Wed Apr 25, 2012 4:49 pm
by skydereign
I assume you set it to loop only once (it is an option in the function), which means the problem is that the event that has the PlaySound2 call is repeating. If you are using PhysicalResponse, then that means the collision event will always repeat (even if you set it to only trigger once per collision). So, assuming you have a jump variable, you could do it like this.
- Code: Select all
if(jump==0)
{
//PlaySound2 goes here
jump=1; // since the collision event is probably where you set jump back to 1
}
It isn't perfect, since you only trigger the sound when the player jumps (unless you implement it so when the player is falling, they can't jump). But without knowing more about how your game is setup, I can't give an accurate answer.
Re: How to play sound just once?
Posted:
Wed Apr 25, 2012 4:58 pm
by lverona
unless you implement it so when the player is falling, they can't jump
I do! I have a
if(yvelocity>=1)
{
cjump=1;//meaning cannot jump
}
So I tried what you suggested myself before, but it does not work. Seems like the function was launched and changing variable will not stop it, only stopping contact with platform...
Isn't there any function that plays a sound just once, without a loop?
Re: How to play sound just once?
Posted:
Wed Apr 25, 2012 5:07 pm
by skydereign
As I said in my previous post, PlaySound2 allows you to specify how many times it loops. If you set it to loop once, it will only play once. If you set it to two, it plays twice. The only time it loops forever is if you set loop to 0. An easy way to tell if it is the sound looping, or the event repeating is to see if the sound continues to play when you jump again. If so, then you set loop to 0, when it should be 1. Otherwise your setting of cjump is too sensitive.
Re: How to play sound just once?
Posted:
Wed Apr 25, 2012 5:58 pm
by lverona
Thank you for advice. Will have to carefully check my conditions then!
Re: How to play sound just once?
Posted:
Thu Apr 26, 2012 3:16 pm
by lverona
I solved this by removing the "jumping in air" check for now. However, I wonder how to achieve one time sounds when collisions occur. Maybe I need a separate variable for that, like "touch" and make it 0 whenever I am in the air. Which, though, is tough, as when you just walk off the platform - how does the game know you are in the air? You haven't jumped.
Re: How to play sound just once?
Posted:
Thu Apr 26, 2012 3:44 pm
by skydereign
I assume your falling code is just too sensitive. I usually use yvelocity>2 or yvelocity>3 before setting jump to 0. If you were using the state method, you could isolate the PlaySound2 call to be just when transitioning between falling/jumping to standing. In general though the method you were trying to use (I assume) could achieve the same effect.
You could use CollisionFree to determine if you are in the air, by checking if a pixel down is free from collisions (only problem is that if you have background actors or similar that you are technically colliding with). But usually to determine if you are in the air is by checking your yvelocity and jump variable.
Re: How to play sound just once?
Posted:
Fri Apr 27, 2012 8:12 am
by lverona
Ha! Man, this is exactly what I did.
So I decided to do this:
1. Added a global variable "stepsound".
2. Added this into the code of the platform so that the sound plays only if stepsound=0, at the same time setting it to 1.
3. Draw Actor script checks for collisionfree y+1 down and if yes - sets stepsound to 1.
The latter point is important because it lets me play the stepping sound even when I just walk off the platform and land on another one without jumping.