Copy link to clipboard
Copied
Hello, I managed to make the sound loop start when the frame loads using this code:
createjs.Sound.on("fileload", handleLoad);
createjs.Sound.alternateExtensions = ["m4a"];
createjs.Sound.registerSound({
id: "audioLoop",
src: "sounds/myAudioLoop.ogg"
});
function handleLoad(event) {
var audioLoopInstance = createjs.Sound.play("audioLoop", {
interrupt: createjs.Sound.INTERRUPT_ANY,
loop: -1
});
};
When I try audioLoopInstance.stop(); or audioLoopInstance.play(); though, nothing seem to happen.
The stop all sounds seem to work, although I don't know how to play it again after that:
stopEverySounds = function () {
return createjs.Sound.stop();
}
stopEverySounds();
I am trying to learn to use the sound instance thing. How do I make audioLoopInstance to start the sound again?
var audioLoopInstance
doesn't exist when your handleLot function completes execution.
try:
var tl=this;
createjs.Sound.on("fileload", handleLoad);
createjs.Sound.alternateExtensions = ["m4a"];
createjs.Sound.registerSound({
id: "audioLoop",
src: "sounds/myAudioLoop.ogg"
});
function handleLoad(event) {
tl.audioLoopInstance = createjs.Sound.play("audioLoop", {
interrupt: createjs.Sound.INTERRUPT_ANY,
loop: -1
});
};
and then you can use
this.audioLoopInstance.sto
...Copy link to clipboard
Copied
var audioLoopInstance
doesn't exist when your handleLot function completes execution.
try:
var tl=this;
createjs.Sound.on("fileload", handleLoad);
createjs.Sound.alternateExtensions = ["m4a"];
createjs.Sound.registerSound({
id: "audioLoop",
src: "sounds/myAudioLoop.ogg"
});
function handleLoad(event) {
tl.audioLoopInstance = createjs.Sound.play("audioLoop", {
interrupt: createjs.Sound.INTERRUPT_ANY,
loop: -1
});
};
and then you can use
this.audioLoopInstance.stop(); // if "this" is the current timeline
or
tl.audioLoopInstance.stop(); // in the frame var tl is defined
or if you want to toggle the sound off and on using an instance t, try
var tl = this;
var n = 0;
this.stop();
createjs.Sound.on("fileload", handleLoad);
createjs.Sound.alternateExtensions = ["m4a"];
createjs.Sound.registerSound({
id: "audioLoop",
src: "z_sounds/chimes.wav"
});
function handleLoad(event) {
tl.audioLoopInstance = createjs.Sound.play("audioLoop", {
interrupt: createjs.Sound.INTERRUPT_ANY,
loop: -1
});
};
this.t.addEventListener("click",tF.bind(this));
function tF(){
if(n%2==0){
this.audioLoopInstance.stop();
} else {
this.audioLoopInstance.play();
}
n++
}
Copy link to clipboard
Copied
Thank you, I didn't know you can only use the var thing only in that frame itself, and that I'd have to use this if the stop()/play() code is in another frame.
Copy link to clipboard
Copied
you're welcome.
as a rule:
use var when you don't want the variable to persist (outside a function if defined in one or outside the current frame if the frame changes).
use "this" if you want the variable to persist.
eg, if attached to a frame:
var var1 = 3; // var1 is undefined outside this frame
this.var1 = 4; // this.var1 is defined throughout the timeline.
function f(){
var var 1 = 33; // var1 is undefined after f completes its code execution
}
eg,
function f(){
if(!var1){
var1 = true; // this line of code will execute every time f is called.
}
}
function f(){
this.var1 = true; // when this line executes, this.var1 is defined inside and outside f and on every frame of the timeline that contains f
}