Copy link to clipboard
Copied
Hi. I'm trying to create a maths game where there are two lots of fish in a tank. You click on a number button to say how many fish there are altogether. The audio says, for example, 'Well done. There are eight'. Then some fish are taken away (from either side) and the player has to click to say how many are left. This continues until all the fish are gone.
The audio is split into three files: 'Well done', 'There are' and 'eight'.
The number of fish is reset elsewhere to the new totals using the variables, var_NumberOfFish1 and var_NumberOfFish2.
I can get this to work the first time, but when some fish are taken away and you try again, when it gets to playing the new number, it plays the correct number and the original number at the same time. This happens every time, so it could end up with three or four numbers being spoken at the same time. Is there a way to reset the audio so it doesn't get played again during that turn?
The code is:
let audioRoot = this
//----------------Set up sounds
function init(){
var assetPath = "sounds/"
var sounds = [
{src:"wellDone.wav", id:"wellDone"},
{src:"thereIs.wav", id:"thereIs"},
{src:"one.wav", id:"one"},
{src:"two.wav", id:"two"} etc...
]
var preload = new createjs.LoadQueue()
preload.addEventListener("fileload", handleFileComplete)
preload.loadFile (assetPath)
function handleFileComplete(event){
createjs.Sound.registerSounds(sounds, assetPath)
wellDonePlay = createjs.Sound.play("wellDone")
thereIsPlay = createjs.Sound.play("thereIs")
onePlay = createjs.Sound.play("one")
twoPlay = createjs.Sound.play("two") etc...
}
//----------------Button One
audioRoot.one_btn.addEventListener("click", oneClicked.bind(this))
function oneClicked () {
if(root.var_ReadyToAnswer){
if(root.var_NumberOfFish1 + root.var_NumberOfFish2 == 1){
wellDonePlay.play()
wellDonePlay.on("complete", function()
{
thereIsPlay.play();
})
thereIsPlay.on("complete", function()
{
onePlay.play();
})
onePlay.on("complete", function()
{
letsPutPlay.play();
})
} else {
tryAgainPlay.play()
}
}
}
}
init()
that line should be in the previous complete function
Copy link to clipboard
Copied
why are there play methods in handleFileComplete?
you should remove those and use:
function oneClicked () {
if(root.var_ReadyToAnswer){
if(root.var_NumberOfFish1 + root.var_NumberOfFish2 == 1){
wellDonePlay=createjs.Sound.play("wellDone")
wellDonePlay.on("complete", function()
{
etc
})
thereIsPlay.on("complete", function()
{
etc
})
onePlay.on("complete", function()
{
etc
})
} else {
etc
}
}
}
}
Copy link to clipboard
Copied
Thanks for the reply. I've changed the code to what you suggested, but now it won't play all three bits of audio. If I use this, for example, it will play, 'There is' and 'one' but then stops. The browser console says, 'undefined is not an object (evaluating audioRoot.onePlay.on).
I've tried different things in front of the variables - 'audioRoot' (defined at the top), 'root' (defined on a different page of code) and 'this', and also without anything, but I get the same error every time. Any thoughts?
{
audioRoot.thereIsPlay = createjs.Sound.play("thereIs")
audioRoot.thereIsPlay.on("complete", function()
{
audioRoot.onePlay = createjs.Sound.play("one")
})
audioRoot.onePlay.on("complete", function()
{
audioRoot.letsPutPlay = createjs.Sound.play("letsPut");
})
}
Copy link to clipboard
Copied
pinpoint which is undefined
Copy link to clipboard
Copied
Copy link to clipboard
Copied
that line should be in the previous complete function
Copy link to clipboard
Copied
Oh, they're nested.
That's worked. Thanks for your help.
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now