Why doesn't the interval start again (trying to start at the bottom, where 'restart.addEventListener (' click ', function ())?

  • 0
    let getY = -40;
    let score = 0;
    let xRand = Math.floor(Math.random()*500);
    let goDown = 0;
    let goDown1 = 0;
    let goDownHiger = 0;
    let canvas = document.getElementById("canvas");
    let ctx = canvas.getContext("2d");
    let speed = 10;
    let startOfGame = false;
    let preStart = false;
    let addressButton = document.getElementById("startGame");
    let restart = document.getElementById("restartGame");
    let activeBrows = false;
    let starr = function(){};
    let z = false;
    let x = false;
    let y = false;
    canvas.width = 500;
    canvas.height = 300;
     
    let loser = new Image();
    loser.src = ('../img/endOfGame.png');
     
    let imgg = new Image();
    imgg.src = ('../img/готоваБомба.png');
     
    let city = new Image();
    city.src = ('../img/cityImg.png');
     
     
     
     let motionOfBomb = function(){
        ctx.clearRect(0,0,500,300);
        ctx.drawImage(city,0,0,500,300);
        ctx.drawImage(imgg,xRand,getY,70,53);
        if(xRand<=70){
            xRand = 70;
        }else if(xRand >= 430){
            xRand = 430 ;
        }
            
        if(getY >= 200){
            startOfGame = false;
            clearInterval(goDown);
            ctx.drawImage(loser,0,0,500,300);
            $('#restartGame').css('display','block');
        };
        
        if(score >= 0 && score <= 10){
            getY++; 
        }else if(score > 10 && score <=15){
            getY = getY + 1.5;
        }else if(score > 15 && score <= 20){
            getY = getY + 2;
        }else if(score > 20 && score <= 25){
            getY = getY + 2.5;
        }else if(score > 25 && score <= 30){
            getY = getY + 3;
        }else if(score > 30 && score <= 40){
            getY = getY + 3.5;
        }else if(score > 40){
            getY = getY + 5;
        }
     };
     
     
     imgg.onload = function(){
        x = true;
     }; 
     city.onload = function(){
        y = true;
     };
     loser.onload = function(){
        z = true;
     }
             
     
     canvas.addEventListener('click', function(e) { 
        let xx = e.pageX - canvas.offsetLeft;
        let yy = e.pageY - canvas.offsetTop;
        if(xx > xRand && xx < xRand + 70 && yy > getY && yy < getY + 53){
            xRand = Math.floor(Math.random()*500);
            getY = -40;
            score++;
            document.getElementById("scoreH1").textContent = "Рекорд: " + score;
        }
     });
      addressButton.addEventListener('click', function(){
        $('#startGame').css('display','none');
        if(x && y){
            startOfGame = true;
        }
        
        if(startOfGame){
            goDown = setInterval(motionOfBomb, speed);
         }
      });
        restart.addEventListener('click', function(){      //При клике интервал должен работать, но нет.
        $('#restartGame').css('display','none');
        if(x && y){
            startOfGame = true;
        }
        
        if(startOfGame){      
            goDown = setInterval(motionOfBomb,10);
        } 
     
      });
    JavaScript Gabriel Evans, Oct 15, 2020

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!