PDA

View Full Version : [AS2] Sound doesn't stop playing


jserete
11-19-2011, 02:20 PM
Help!
I need help with a problem:

* I try to call a function which calls a sound for my game when a player hits an object
* I set an if statement to try and stop it once it plays
* I'm not sure if that's working

Anyway,
Here's the code

var sfxNum = 0;
onEnterFrame = function ()
{
if (_root.a1.s == true or _root.a2.s == true or _root.a3.s == true or _root.a4.s == true or _root.a5.s == true or _root.a6.s == true or _root.a7.s == true)
{
playSoundOnce();
}
if (dead == false)
{
firstMusic == 0 ? (mm0.start(), firstMusic = 1) : null;
MUSIC ? mm0.setVolume(40) : mm0.setVolume(0);
count++;
}
};

SFX = true;
sss_loopSound;
MUSIC = true;
firstMusic = 0;
this.createEmptyMovieClip("m0",100);
mm0 = new Sound(m0);
mm0.attachSound("bg0");
mm0.onSoundComplete = function()
{
mm0.start();
};

function playSound(id:String, vol:Number, loopVersion:Boolean)
{
if (SFX)
{
if (!loopVersion)
{
this.createEmptyMovieClip("sss" + soundNum,10500 + soundNum);
var __reg2 = new Sound(this["sss" + soundNum]);
__reg2.attachSound(id);
__reg2.setVolume(vol);
__reg2.start();
soundNum >= 30 ? (soundNum = 0) : soundNum++;
}
else
{
if (!sss_loopVersion)
{
this.createEmptyMovieClip("sss_loopVersion",10531);
sss_loopSound = new Sound(this["sss_loopVersion"]);
sss_loopSound.attachSound(id);
sss_loopSound.setVolume(vol);
sss_loopSound.start();
}
sss_loopSound.onSoundComplete = function()
{
sss_loopSound.start();
};
}
}
}
function playSoundOnce()
{
if(sfxNum<1){
trace("sfxNum<1 and sfxNum = "+sfxNum);
playSound("3",70,false);
sfxNum++;
trace("now sfxNum = "+sfxNum);
}
}

jserete
11-19-2011, 02:47 PM
Hey guys!

I think the problem doesn't have to do anything with my code (because I took out the code for playing the sounds and it still loops) but...

I'm not sure where to start...

anyway

here's the link to the fla (sorry if it's a bit big or anything guys, please bear with me...)

http://s000.tinyupload.com/?file_id=33575835256577294126

audiopro
11-19-2011, 03:13 PM
I would say that the problem is with the code.
If you take away the code, you don't have a problem :)

If the sound keeps firing, trace the values of all the a?.s variables, as one of them must be true to fire the function.

jserete
11-19-2011, 11:18 PM
The thing is, I just want it to play once

I already tried removing the playSound and playSoundOnce functions but it still plays,

I tried looking into the mc that set a*.s to true but not one has any code related to sound,

instead of using the a*.s values, I switched to using a hitTest, but it still plays endlessly (and annoyingly :().

In fact, I don't think it has anything to do with the code, but if there's something that I missed anywhere, I'd appreciate it if you pointed it out

P.S

The FLA is always there

audiopro
11-20-2011, 06:57 AM
15mB of Flash, what on earth have you got in there :)

Is this bit of code still in?
this.createEmptyMovieClip("m0",100);
mm0 = new Sound(m0);
mm0.attachSound("bg0");
mm0.onSoundComplete = function()
{
mm0.start();
};

jserete
11-20-2011, 08:59 AM
Yes, it's still there. And the size of the fla is due to the sound files.. the game w/out the sounds is roughly 200 kb :p

P.S.

I just used the function that Scearezt showed me... not sure why that's there

audiopro
11-20-2011, 09:14 AM
Try commenting out
mm0.start();
as it looks like it once the sound finishes, that line starts it again.

jserete
11-20-2011, 09:16 AM
I don't even use that for anything else other than that bg0 sound... and I don't even have a sound with that linkage name! (like I said, it was Scearezt that showed me the function)

Nonetheless, you're the expert :)

I'll try it! (tomorrow)

audiopro
11-20-2011, 09:23 AM
If you do not use a sound named "bg0" then the function is redundant and should be removed.
Something somewhere is causing the sound to play.
Is the code all together or scattered around numerous of MC's

jserete
11-20-2011, 09:54 AM
All the code for sounds (that I know of, I still have to double-check some MCs)
is on the main frame (frame 30).. the rest of the sounds are manually/physically placed on the timeline..

However, the code for the game is scattered (which, I know, is inconvenient) but the bulk of it is on the main frame..

jserete
11-22-2011, 10:54 AM
umm nothing's working yet...

audiopro
11-22-2011, 11:00 AM
The best way would be to go through all the code and find where the sound command is. A messy alternative would be to rename the sound file which is causing you the problem.

jserete
11-23-2011, 09:20 AM
Alright... i suppose renaming would be fine..., but let's say if


gotoAndPlay(3);


on frame 6 and


playSound("sound");


on frame 3

would that sound still play over and over

and if the same code was there but the sound was placed manually on frame 3-6 (which keeps repeating) would there be a repeating sound?

audiopro
11-23-2011, 03:11 PM
would that sound still play over and over
I have no idea, why don't you try it and let us know.

I would go through all of the objects 1 by one and comment out all of the sound commands. Once no sound is playing, reintroduce them one at a time where you want the sound to play.

jserete
11-24-2011, 08:14 AM
Alright, thanks!

In the meantime I'll try renaming the sound, and removing the manually placed sounds

jserete
11-25-2011, 12:58 PM
UPDATE : Renaming doesn't work, and neither does taking away the linkage name. The error is most likely because of a manually placed sound. Will try and see what happens when you take away the sound...

jserete
11-26-2011, 03:57 AM
UPDATE: LOL! What happened? I just removed the sound and suddenly everything's working perfectly. Thanks anyway!