Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

javascript - Stop playing sound before going on to next function

I was tasked to create a simple mini game based solely on Javascript. I encountered a problem as I was adding in sound effects..

After the player drowns, the game should 'return' to its initial frame with one 'live' count decreased. During the drowning action, it plummets down the lake while the sound effect comes into place. However, the sound is activated a few .seconds late and the player freezes at the bottom of the lake. The game does not return either.

I have also tried adding stop() but it doesn't work. Here's a fragment of the relevant code.

gameCharacter_y +=2;
drownSound.play();
    
    
return;

Been stuck for days now and am an absolute beginner, would appreciate and be grateful for any suggestions! Thanks.

question from:https://stackoverflow.com/questions/65948237/stop-playing-sound-before-going-on-to-next-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to manipulate sound you should have a soundmanager... This makes it really easy to see what clips are playing and pause them and do with them as you please! I'm not sure how to make one in JavaScript... But what you are looking for is...

drownSound.pause();

There is no 'stop()' method apparently...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...