Participant
July 17, 2024
Question
Help with sound on Animate (Loop and Scene Switching) AS3
- July 17, 2024
- 1 reply
- 295 views
Hello how's everyone doing? I'm trying to achieve adding background music to a project. The timeline is pretty empty, it has 1 keyframe for each layer. I need to import this song and to make it repeat on loop infinitely until it loads another scene on the main game that I think is coded on "Visual Studio". All the scenes are individual .fla I don't have access.
I imported the sound and linked as "BackgroundMusic". As I didn't find any tutorials, I prompted ChatGpt and gave me this code:
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.Event;
// Create a Sound object and a SoundChannel object
var bgMusic:BackgroundMusic = new BackgroundMusic();
var bgMusicChannel:SoundChannel;
// Function to start playing the music
function playMusic():void {
// Stop any previously playing sound
if(bgMusicChannel) {
bgMusicChannel.stop();
}
// Play the new sound in a loop
bgMusicChannel = bgMusic.play(0, int.MAX_VALUE);
}
// Function to stop playing the music
function stopMusic(event:Event):void {
if(bgMusicChannel) {
bgMusicChannel.stop();
}
}
// Start playing the music when the scene is loaded
playMusic();
// Stop the music when the scene is removed from the stage
this.addEventListener(Event.REMOVED_FROM_STAGE, stopMusic);
Thanks to everyone taking the time to read and help ♥
