بازی صدا

ਸੋਣੀ ਬਿਜਲੀ ਵਧਾਓ। ਜਦੋਂ ਲਾਲ ਚੌਕਟ ਰੁਕਾਵਟ ਨਾਲ ਟਕਰਾਵੇ ਤਾਂ ਤੁਸੀਂ ਟੱਕਰਾਵੇ ਦੀ ਆਵਾਜ ਸੁਣਿਆ ਹੈ ਕਿ ਨਹੀਂ?






ਸੰਗੀਤ ਕਿਵੇਂ ਜੋੜਨਾ ਹੈ?

ਤੁਹਾਡੇ ਗੇਮ ਵਿੱਚ ਸੰਗੀਤ ਅਤੇ ਸ਼ਬਦ ਜੋੜਨ ਲਈ HTML5 <audio> ਐਲੀਮੈਂਟ ਵਰਤੋਂ ਕਰੋ。

ਹੇਠ ਦੇ ਉਦਾਹਰਣ ਵਿੱਚ, ਅਸੀਂ ਨਵਾਂ ਆਬਜੈਕਟ ਕੰਸਟਰਕਟਰ ਬਣਾਉਣ ਲਈ ਇੱਕ ਨਵੀਂ ਫੰਕਸ਼ਨ ਬਣਾਈ ਹੈ ਕਿ ਸੰਗੀਤ ਆਬਜੈਕਟ ਨੂੰ ਹੱਲਾਸ਼ੇਰੀ ਦੇਵੇ:

ਇੱਕੋ ਵਾਰ

function sound(src) {
  this.sound = document.createElement("audio");
  this.sound.src = src;
  this.sound.setAttribute("preload", "auto");
  this.sound.setAttribute("controls", "none");
  this.sound.style.display = "none";
  document.body.appendChild(this.sound);
  this.play = function(){
    this.sound.play();
  }
  this.stop = function(){
    this.sound.pause();
  }
}

ਜੇਕਰ ਤੁਸੀਂ ਨਵਾਂ ਸੰਗੀਤ ਆਬਜੈਕਟ ਬਣਾਉਣਾ ਚਾਹੁੰਦੇ ਹੋ, ਤਾਂ ਸੰਗੀਤ ਕੰਸਟਰਕਟਰ, ਜਦੋਂ ਲਾਲ ਚੌਕਟੀ ਰੁਕਾਵਟ ਨਾਲ ਟਕਰਾਵੇ, ਤਾਂ ਸੰਗੀਤ ਚਲਾਓ:

ਇੱਕੋ ਵਾਰ

var myGamePiece;
var myObstacles = [];
var mySound;
function startGame() {
  myGamePiece = new component(30, 30, "red", 10, 120);
  mySound = new sound("bounce.mp3");
  myGameArea.start();
}
function updateGameArea() {
  var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  for (i = 0; i < myObstacles.length; i += 1) {
    if (myGamePiece.crashWith(myObstacles[i])) {
      mySound.play();
      myGameArea.stop();
      return;
    }
  }
...
}

خودتان امتحان کنید

ਬੈਕਗਰਾਊਂਡ ਸੰਗੀਤ

ਜੇਕਰ ਤੁਸੀਂ ਬੈਕਗਰਾਊਂਡ ਸੰਗੀਤ ਜੋੜਨਾ ਚਾਹੁੰਦੇ ਹੋ, ਤਾਂ ਨਵਾਂ sound ਆਬਜੈਕਟ ਜੋੜੋ ਅਤੇ ਜੇਕਰ ਤੁਸੀਂ ਗੇਮ ਸ਼ੁਰੂ ਕਰਦੇ ਹੋ, ਤਾਂ ਸੰਗੀਤ ਚਲਾਓ:

ਇੱਕੋ ਵਾਰ

var myGamePiece;
var myObstacles = [];
var mySound;
var myMusic;
function startGame() {
  myGamePiece = new component(30, 30, "red", 10, 120);
  mySound = new sound("bounce.mp3");
  myMusic = new sound("gametheme.mp3");
  myMusic.play();
  myGameArea.start();
}

خودتان امتحان کنید