Skip to content

Commit

Permalink
Merge pull request #810 from Aathish101/patch-14
Browse files Browse the repository at this point in the history
Update script.js
  • Loading branch information
Ayushparikh-code authored Oct 27, 2024
2 parents c4f790d + ca723d8 commit 5402bba
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions Animated Car/Animated Car/script.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
// const audio = new Audio('caraudio.mp3');
// var resp = audio.play();

// if(resp != undefined){
// resp.then(_ => {
// //autoplay starts
// }).catch(error => {
// //shoe error
// });
// }
// audio.loop()=true;

var audio = document.createElement("audio");
document.body.appendChild(audio);
audio.src="caraudio.mp3"
audio.src = "caraudio.mp3";

let isMoving = false;

// Start playing the audio when the mouse moves
document.body.addEventListener("mousemove", function() {
if (!isMoving) {
audio.play();
audio.loop = true; // Set the audio to loop
isMoving = true; // Indicate that the car is moving
}
});

// Stop playing the audio when the left mouse button is released
document.body.addEventListener("mouseup", function(event) {
if (event.button === 0) { // 0 is the left mouse button
audio.pause();
audio.currentTime = 0; // Optionally reset audio to the beginning
isMoving = false; // Indicate that the car has stopped
}
});

document.body.addEventListener("mousemove", function(){
audio.play();
audio.loop();
})
// Optional: Start playing audio again if the mouse is moved while stopped
document.body.addEventListener("mousedown", function() {
if (!isMoving) {
audio.play();
audio.loop = true;
isMoving = true;
}
});

0 comments on commit 5402bba

Please sign in to comment.