00:00
00:00
Newgrounds Background Image Theme

Kingkavvvv just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3:Sound overlapping self

484 Views | 2 Replies
New Topic Respond to this Topic

AS3:Sound overlapping self 2016-07-12 01:57:28


Hello! So, here's my situation: I'm working on a flash project in which a song is playing in a menu. You can click a button to go to the next frame, frame 2. If you press a button on frame 2, it brings you back to frame 1. At that point, the song will start playing again, causing multiple instances of the song to start playing. I'd like to find out how I can go about having the song just play and loop normally, unaffected by the user returning to the first frame. One other thing I should mention is that I'm doing it this way rather than just having the sound play on another layer on the timeline because I need the sound channel set up for a later section in which a movie clip plays a short animation with sound, and the music must be turned off at that point. Plus, plain and simple, it's nice to learn a little more about AS3 and have nice, clean solutions to these things rather than half-assing frames and layers around. Here's my code so far, along with notes on what my logic was for each step:

var mySound:music = new music();
var myChannel:SoundChannel = new SoundChannel();
// Setting the sound and channel up. 
if (myChannel.position > 0) {
	var isPlaying:Boolean = true;	
}
// Setting up a variable to determine if if a channel's playing.
if (!isPlaying) {
	mySound.play(); 
	trace("starting up");
}
// If it's not already playing, then do it!
if (isPlaying) {
	myChannel.stop();
	trace("turnoff");
	isPlaying = false; 
}
//When the frame is entered a second time, a second instance of mySound will start playing. The purpose of this section is to stop that instance. So far, it's been unaffective; I'm still only getting "starting up" in the output each time I return to the frame.

In a nutshell, I'm thinking that this might be a logic issue that I'm having, or maybe I'm going about this whole thing the wrong way...I found a bunch of other people having this issue on Google, but none of those solutions worked the way I needed them to. Thanks a ton to anyone who can help!

Response to AS3:Sound overlapping self 2016-07-12 12:07:18


This is happening because of how frames work, which are not designed to be used in game development (they're meant for animations). So, the best solution would be to not use frames and instead use a state machine to control everything, such as what sounds should play in which states and when. Another good step forward would be to not use the Adobe Flash IDE for developing games and instead use something like Flash Develop, which is much better suited for programming—Adobe Flash is an animation suite, not a development suite, after all.

If you want to stick to using frames, which I again do not recommend, you would need to use a global boolean that is accessible in both frames (or any others where applicable) to determine if the sound should be played or not. I'm guessing the code you posted is on the first frame, which is why it keeps replaying: every time it enters the frame, all of those variables are being newly created.

Response to AS3:Sound overlapping self 2016-07-15 01:47:07


At 7/12/16 12:07 PM, Diki wrote: This is happening because of how frames work, which are not designed to be used in game development (they're meant for animations). So, the best solution would be to not use frames and instead use a state machine to control everything, such as what sounds should play in which states and when. Another good step forward would be to not use the Adobe Flash IDE for developing games and instead use something like Flash Develop, which is much better suited for programming—Adobe Flash is an animation suite, not a development suite, after all.

If you want to stick to using frames, which I again do not recommend, you would need to use a global boolean that is accessible in both frames (or any others where applicable) to determine if the sound should be played or not. I'm guessing the code you posted is on the first frame, which is why it keeps replaying: every time it enters the frame, all of those variables are being newly created.

Thanks for the advice! Sorry it took so long for me to post again, I usually don't respond to posts like these until I've made some progress or really need more help. It's actually kind of funny, I started doing research on all the things you suggested, and started stumbling upon things I hadn't even thought of trying yet...I ended up going with a couple of event listeners to get the job done. I know a hell of a lot more about AS3 now than I thought I'd manage to learn in months, let alone a week. Thanks again!