Why the background sound keep repeating? - actionscript-3

I have one background sound to use in my project. But when I click the button to other scene, the sound repeats themselves and worse, it redundant with the sound in next scene. I was trying to use one background song for the whole project but this problem happened. Can teach me how?
I am using Adobe Flash CS6. Thank you.

the sound is repeating itself because timeline is repeating by default and you should add command stop() in actionscript. You did not provide any source code so I'll assume you don't have any, in your case here is a sample program in actionscript that loops the background music.
import flash.events.MouseEvent;
stop(); //<-- make sure to stop timeline
var bgMusic:Sound = new BGMusic();
var bgChannel:SoundChannel = new SoundChannel();
bgChannel = bgMusic.play();
bgChannel.addEventListener(Event.SOUND_COMPLETE, loop);
startButton.addEventListener(MouseEvent.CLICK, startGame);
function startGame(e:MouseEvent):void {
gotoAndStop(1, "Scene 2"); //<-- stop after clicking button
}
function loop(e:Event):void {
bgChannel = bgMusic.play();
bgChannel.addEventListener(Event.SOUND_COMPLETE, loop);
}

Related

Custom Mouse Cursor dropping duplicate symbols after its been removed

first of all, I'm a total noob to as3 and coding in general, I barely operate outside of code snippets.
I'm working on a project, and part of which is a scene where you get a custom mouse cursor upon entering the scene, and when you leave the scene, the custom mouse cursor is removed. The code I'm using to start the custom cursor is:
stage.addChild(crsTemple);
crsTemple.mouseEnabled = false;
crsTemple.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor);
function fl_CustomMouseCursor(event:Event)
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
}
Mouse.hide();
with crsTemple being the instance name for the custom cursor. Then, when a new scene is entered (via rolling over an object), i have the following code in the new scene:
stage.addChild(crsTemple);
crsTemple.mouseEnabled = false;
crsTemple.addEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_4);
function fl_CustomMouseCursor_4(event:Event)
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
}
Mouse.hide();
crsTemple.removeEventListener(Event.ENTER_FRAME, fl_CustomMouseCursor_4);
stage.removeChild(crsTemple);
Mouse.show();
Unfortunately, whenever I go into the second scene, I get the regular mouse again, but it drops the crsTemple wherever the mouse was when the scene change happened, and it stays there for the rest of the time the file is running.
Any help is greatly appreciated, much thanks in advance for helping a noob like me!
No need to write the same code in new Scene. You can actually use all declarations form the first Scene. In the following code snippet MOUSE_MOVE handler (fl_CustomMouseCursor) from scene 1 will be called in scene 2 either. Custom cursor will also be accessible by its name crsTemple.
import flash.display.MovieClip;
import flash.events.MouseEvent;
var crsTemple:Sprite = new CrsTemple();
crsTemple.mouseEnabled = false;
addChild(crsTemple);
// for smooth cursor movement MOUSE_MOVE instead of ENTER_FRAME
stage.addEventListener(MouseEvent.MOUSE_MOVE, fl_CustomMouseCursor);
stage.addEventListener(MouseEvent.CLICK, nextStage); // for test purpose, just to switch the stage
function fl_CustomMouseCursor(event:Event):void
{
crsTemple.x = stage.mouseX;
crsTemple.y = stage.mouseY;
trace(crsTemple.x);
}
function nextStage(e:Event):void {
gotoAndStop(1,"Scene 2");
}
Mouse.hide();
stop();
here is a link to fla sample

Looping sound in AS3

Trying to loop a background soundtrack while my flash program is in use.
So far my code is this:
//turn off sound
btnOff.addEventListener(MouseEvent.CLICK, fl_stopsound);
function fl_stopsound(event:MouseEvent):void
{
SoundMixer.stopAll();
}
//turns sound on
btnOn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound_1);
var fl_SC_1:SoundChannel;
//keeps track of whether the sound should be played or stopped
var fl_ToPlay_1:Boolean = true;
function fl_ClickToPlayStopSound_1(evt:MouseEvent):void
{
var mySound:Sound = new background();
mySound.play();
}
where btnOff turns off the sound and btnOn turns on the sound. My soundtrack is 1:50min long. Is it possible to loop the track within the program with these buttons?
Cheers
Use mySound.play(0, int.MAX_VALUE);

Adobe Flash/ActionScript 3.0 moving to next scene but nothing plays

I've searched endlessly for an answer but can't seem to find one.
I'm building a flash presentation for a client. It's got about 15 scenes, the .fla file is 200MB before publishing (26MB afterward). It's a pretty simple operation; I'have a pause/play & replay button, and once the scene is completed a next & replay button appears in the center of the stage.
My issue is when I get to about the 8th scene, halfway through. All my tweens and buttons stop working. Simple fade-in text doesn't come up and my pause/play & replay no longer function. I've tried shifting around scenes to see if it was anything in particular, but no matter what order and what scenes it always stops halfway through the 8th. I don't get any error notifications before, after or during the play. Tracing tells me that the button has been clicked and it's moved to the next scene but it simply will not play. Adding a play(); comment at the first frame of the next scene has not helped either.
Here are my functions that are on Scene 1 Frame 1
function pause_movie(event:MouseEvent):void {
stop();
playBtn.visible = true;
pauseBtn.visible = false;
}
function play_movie(event:MouseEvent):void {
play();
playBtn.visible = false;
pauseBtn.visible = true;
}
function replay_movie(event:MouseEvent):void {
gotoAndPlay(1);
}
function next_movie(event:MouseEvent):void {
this.nextScene();
trace("next_movie " + this.currentScene.name);
}
And then I just add event listeners when my buttons appear per scene
import flash.events.MouseEvent;
//Hide play button to start
playBtn.enabled = true;
pauseBtn.enabled = true;
playBtn.visible = false;
pauseBtn.addEventListener(MouseEvent.CLICK, pause_movie);
playBtn.addEventListener(MouseEvent.CLICK, play_movie);
replayBtn.addEventListener(MouseEvent.CLICK, replay_movie);
Any help is appreciated! Thank you!
I've created a new flash file with a blank canvas (just page number and next button). I'm loading the audio externally now as per #VC.One 's comment and the program stops working at the same scene regardless of which audio file I put in there.
Here is my updated code:
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
var channel:SoundChannel = new SoundChannel;
var s:Sound = new Sound();
function newSound() {
s.removeEventListener(Event.COMPLETE, onSoundLoaded);
s = new Sound();
s.addEventListener(Event.COMPLETE, onSoundLoaded);
}
function onSoundLoaded(e:Event):void
{
channel = s.play();
}
function next_movie(event:MouseEvent):void
{
channel.stop();
try {
s.close();
}
catch(e:Error) {
trace("File already loaded");
}
this.nextScene();
trace("next_movie " + this.currentScene.name);
}
and each scene starts with:
pageNum.text = this.currentScene.name;
skipBtn.addEventListener(MouseEvent.CLICK, next_movie);
newSound();
s.load(new URLRequest('audio/ISB page 17 tk 1.mp3'));
Finally solved this issue. The scenes added together were more than the 16k frames Flash is apparently limited to. I had to convert each scene into a movieclip and put them in each of their own frame all in one scene with a bit of coding to play each one after the previous one finished. Everything works fine now. Thanks guys!

Sound won't stop when I go back to a previous Stage with code

i have 4 scenes and i put my script for music in scene 2 and then i jump to scene 3 but when i go back to scene 2 and press pause or stop, my music wont do that, but when i play, the music play and i get 2 music start :(, can anybody help the script ?
regards im newbie,
stop();
import flash.events.MouseEvent;
import flash.media.SoundChannel;
//declaring all variables
var isPlaying:Boolean = false;// boolean type of variables can be true or false only
var myMusic = new soothing();// saving music in a varaible
var myChannel:SoundChannel = new SoundChannel();// sound channel Class to stop
var lastPosition:Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
stop_btn.addEventListener(MouseEvent.CLICK, onStopClick);
function onPlayClick(event:MouseEvent):void
{
if (isPlaying == false)
{
isPlaying = true;
myChannel = myMusic.play(lastPosition);
}
myChannel.addEventListener(Event.SOUND_COMPLETE, completeHANDLER);
function completeHANDLER(event:Event):void
{
lastPosition = 0;
isPlaying = false;
}
}
function onPauseClick(event:MouseEvent):void
{
isPlaying = false;
lastPosition = myChannel.position;
myChannel.stop();
}
function onStopClick(event:MouseEvent):void
{
if (isPlaying == true)
{
isPlaying = false;
lastPosition = 0;
myChannel.stop();
}
}
homebtn.addEventListener(MouseEvent.CLICK, qnextScene);
function qnextScene(event:MouseEvent):void
{
gotoAndStop(1, "Scene 2");
}
gallerybtn.addEventListener(MouseEvent.CLICK, nxtScene);
function nxtScene(event:MouseEvent):void
{
gotoAndStop(1, "Scene 3");
}
mebtn.addEventListener(MouseEvent.CLICK, nextsScene);
function nextsScene(event:MouseEvent):void
{
gotoAndStop(1, "Scene 4");
}
Ahhh, yes, the infamous stage sound bug I spent four months chasing down.
One fix: DON'T GO BACKWARDS ON STAGES. They're quite buggy.
Instead, what you put on a given stage, instead put inside a MovieClip. Create two functions instead each of those major MovieClips, one to start all the sounds, animations, etc that need to happen when it appears. The other to STOP sounds, animations, etc.
Then, set up a SINGLE stage with code to swap between these.
One other issue you're going to run into when you start in on this approach is that variables will seemingly clear themselves spontaneously. This is due to the nature of the timeline.These master movieclips should have ONE FRAME ONLY, and stop(); must be the first line of code after the import. If you have multiple frames on these master movieclips, or if you neglect stop, things will keep re-initializing, which is a pain in the butt.
(Heading this off at the pass: yes, code on the timeline is
acceptable, as long as all the code relates directly to the object it
is on the timeline OF. Pure code should be parked in the document
class and linked classes. The idea that you should never put code on
the timeline is a relic practice from AS2.)

Stopping the sound of a child swf

The timeline of my child swf, there are layers with specific sound files in them to be in sync with my animation. Now the problem arises when I try to import this swf into my main flash website using a loader and the sound will continuously play everytime the button is clicked in the parent. My question is how do I get the sound to completely clear itself and restart from frame 0 of the child swf upon every click of the button that loads the child into the loader within the parent.
var myLoader:Loader = new Loader();// create a new instance of the Loader class
var project1:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/Greeting_Card/GreetingCard.swf");
var project2:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/Landscape/Landscape.swf");
var project3:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/SpaceInvadersTribute/Main.swf");
var project4:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/RandomImageProducer/RndImgProd.swf");
//var project5:URLRequest = new URLRequest("Projects/Q1/Flash_Projects/Tutorial/Main.swf");
var project6:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/Soundboard/Main.swf");
btnQ1P1.addEventListener(MouseEvent.CLICK,Greeting);
btnQ1P2.addEventListener(MouseEvent.CLICK,landscape);
btnQ1P3.addEventListener(MouseEvent.CLICK, tribute);
btnQ1P4.addEventListener(MouseEvent.CLICK, slideshow);
//btnQ1P5.addEventListener(MouseEvent.CLICK, tutorial);
btnQ1P6.addEventListener(MouseEvent.CLICK, soundboard);
addChild(myLoader);
function Greeting(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project1);
myLoader.x=550;
myLoader.y=130;
}
//Errors with Sound clips
function landscape(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project2);
myLoader.x=440;
myLoader.y=130;
}
function tribute(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project3);
myLoader.x=550;
myLoader.y=170;
}
//Errors with slideshow Code!
function slideshow(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project4);
myLoader.x=530;
myLoader.y=130;
}
//function tutorial(event:MouseEvent):void{
//SoundMixer.stopAll();
//myLoader.unload();
//myLoader.load(project5);
//myLoader.x = 440;
//myLoader.y = 130;
//}
function soundboard(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project6);
myLoader.x=550;
myLoader.y=130;
}
I had this same problem.
You need to change the sound from "Event" to "Stream". In Flash Professional, this can be accessed from the properties tab of the sound on the timeline. Click on the sound, and then go to the Properties tab and change the type from Event to Stream.
Now, the sound will stop when the timeline is stopped.