'Sound problem in "for" loop when Player win Javascript
Sound problem. In the "for" loop, a sound has been assigned when the car hits the hitbox of the package to be taken ('pacz'). This is counted as a point. Then the sound play: 'good'. Individually for each collected package. The problem arises when the player collects all 9 packs. The "good" sound then comes on many times at the same time. Is there any way to make the sound of the 9 pack sound 'good' play once at the end of the game?
var root = this
var collectedBoxes = 0
root.mainPacz.gotoAndStop(boxArray[0])
carCollisionSetup()
boxCollisionSetup()
var gameover = false
function boxCollisionCheck() {
let carBounds = root.car.getTransformedBounds();
let infoBox = root.mainPacz
for (i = 1; i < 10; i++) {
let boxBounds = root['pacz' + i].getTransformedBounds()
if (carBounds.intersects(boxBounds) && infoBox.timeline.position == root['pacz' + i].timeline.position) {
takeAnim(root['pacz' + i])
collectedBoxes++
createjs.Sound.play('good')
console.log('good w pacz')
if (collectedBoxes < 9) {
infoBox.gotoAndStop(boxArray[collectedBoxes])
} else {
if(!gameover)winFunction()
console.log(winFunction)
console.log('wrong pacz else')
}
}
}
}
function winFunction() {
gameover = true
console.log("=========WIN========")
setTimeout (() =>{
createjs.Sound.play('swietnie')
},1500)
setTimeout (() =>{
root.play();
},5000)
stopCycle()
root.stoper.play()
//clearInterval(carPosChange)
//timerTimeMS = 0
}
//ANCHOR Handling losing
function loseFunction() {
console.log("=========LOSE========")
}
End of game with sounds looks like (These "consol.log" are called a lot of times.): enter image description here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
