Javascript 如何添加稍后将用于if语句以执行重新加载的计数器

Javascript 如何添加稍后将用于if语句以执行重新加载的计数器,javascript,Javascript,我不确定如何正确设置游戏,所以我正在计算敌人。如果我可以计算敌人的数量,我相信我可以设置我的代码,这样我就可以进行确认,这样我就可以重新加载页面,并可能创建更多的敌人 我认为我的问题是基于我的enemycount变量。我不知道如何倒计时到零。我还认为我的问题可能与没有基于EnemCount的函数有关 //This is the Variables that I have used for code as well as the codes based around the enemies.//

我不确定如何正确设置游戏,所以我正在计算敌人。如果我可以计算敌人的数量,我相信我可以设置我的代码,这样我就可以进行确认,这样我就可以重新加载页面,并可能创建更多的敌人

我认为我的问题是基于我的enemycount变量。我不知道如何倒计时到零。我还认为我的问题可能与没有基于EnemCount的函数有关

//This is the Variables that I have used for code as well as the codes based around the enemies.//  
var canvasBg =document.getElementById("canvasBg"),
    ctxBg = canvasBg.getContext("2d"),
    canvasEntities =document.getElementById("canvasEntities"),
    ctxEntities = canvasEntities.getContext("2d"),
    canvasWidth = canvasBg.width,
    canvasHeight = canvasBg.height,
    player1 = new Player(),
    enemies = [],
    numEnemies = 5,
    enemycount = numEnemies,
    obstacles = [],
    isPlaying = false,

function Enemy(){
    this.srcX = 140;
    this.srcY = 600;
    this.width = 45;
    this.height = 54;
    this.drawX = randomRange(0, canvasWidth - this.width);
    this.drawY = randomRange(0, canvasHeight - this.height);
    this.centerX = this.drawX + (this.width / 2);
    this.centerY = this.drawY + (this.height / 2);
    this.targetX = this.centerX;
    this.targetY = this.centerY;
    this.randomMoveTime + randomRange(4000, 10000);
    this.speed = 1;
    var that = this;
    this.moveInterval =setInterval(function(){that.setTargetLocation();}, that.randomMoveTime);
    this.isDead = false;
}

Enemy.prototype.update = function (){
    this.centerX = this.drawX + (this.width / 2);
    this.centerY = this.drawY + (this.height / 2);
    this.checkDirection();
};

Enemy.prototype.draw = function(){
    ctxEntities.drawImage(imgSprite, this.srcX, this.srcY, this.width, this.height, this.drawX, this.drawY, this.width, this.height);


};



function initEnemies(){
    for(var i = 0; i < numEnemies; i++){
        enemies[enemies.length] = new Enemy();
    }
}

function updateAllEnemies(){
    for(var i = 0; i < enemies.length; i++){
        enemies[i].update();
    }
}
function drawAllEnemies(){
    for(var i = 0; i < enemies.length; i++){
        enemies[i].draw();
    }
}


Enemy.prototype.setTargetLocation = function(){
    this.randomMoveTime = randomRange(4000, 10000);
    var minX = this.centerX - 50, 
        maxX = this.centerX + 50,
        minY = this.centerY - 50,
        maxY = this.centerY + 50;
    //no ghosts off canvas
    if(minX < 0){
        minX = 0;
    }
    if(maxX > canvasWidth){
        maxX = canvasWidth;
    }
    if(minY < 0){
        minY = 0;
    }
    if(maxY > canvasHeight){
        maxY = canvasHeight;
    }
    this.targetX = randomRange(minX, maxX);
    this.targetY = randomRange(minY, maxY);
};

Enemy.prototype.checkDirection = function(){
    if(this.centerX < this.targetX){
        this.drawX += this.speed;
    }else if(this.centerX > this.targetX){
        this.drawX -= this.speed;
    }
    if(this.centerY < this.targetY){
        this.drawY += this.speed;
    }else if(this.centerY > this.targetY){
        this.drawY -= this.speed;
    }
};

Enemy.prototype.die = function (){
    var soundEffect = new Audio("audio/dying.wav");
    soundEffect.play();
    clearInterval(this.moveInterval);
    this.srcX = 185;
    this.isDead = true;
    //this.enemycount -=;
};

function collision(a, b){
    return a.drawX <= b.drawX + b.width &&
        a.drawX >= b.drawX &&
        a.drawY <= b.drawY + b.height &&
        a.drawY > b.drawY;
}

if (enemycount == 0){
    alert("WELL DONE");

    // confirm('Congrats \n Level Complete!!! \n Play Again')location.reload();
}
//这是我在代码中使用的变量,以及基于敌人的代码。//
var canvasBg=document.getElementById(“canvasBg”),
ctxBg=canvasBg.getContext(“2d”),
canvasEntities=document.getElementById(“canvasEntities”),
ctxEntities=canvasEntities.getContext(“2d”),
画布宽度=画布bg.width,
画布高度=画布高度,
player1=新玩家(),
敌人=[],
numEnemies=5,
enemycount=numEnemies,
障碍=[],
isplay=false,
功能敌人(){
这个.srcX=140;
这个.srcY=600;
这个宽度=45;
这个高度=54;
this.drawX=随机范围(0,画布宽度-this.width);
this.drawY=随机范围(0,画布高度-this.height);
this.centerX=this.drawX+(this.width/2);
this.centerY=this.drawY+(this.height/2);
this.targetX=this.centerX;
this.targetY=this.centerY;
这个.randomMoveTime+randomRange(400010000);
这个速度=1;
var=这个;
this.moveInterval=setInterval(函数(){that.setTargetLocation();},that.randomMoveTime);
this.isDead=false;
}
defey.prototype.update=函数(){
this.centerX=this.drawX+(this.width/2);
this.centerY=this.drawY+(this.height/2);
这是checkDirection();
};
defey.prototype.draw=函数(){
ctxenties.drawImage(imgSprite,this.srcX,this.srcY,this.width,this.height,this.drawX,this.drawY,this.width,this.height);
};
函数init(){
对于(变量i=0;i画布宽度){
maxX=画布宽度;
}
if(minY<0){
minY=0;
}
如果(最大>画布高度){
maxY=画布高度;
}
this.targetX=随机范围(minX,maxX);
this.targetY=随机范围(minY,maxY);
};
defey.prototype.checkDirection=函数(){
如果(this.centerXthis.targetX){
this.drawX-=this.speed;
}
if(this.centerYthis.targetY){
this.drawY-=this.speed;
}
};
defey.prototype.die=函数(){
var soundEffect=新音频(“Audio/diing.wav”);
音效。播放();
clearInterval(此.moveInterval);
这个.srcX=185;
this.isDead=true;
//this.enemycount-=;
};
函数冲突(a,b){
返回a.drawX=b.drawX&&
a、 拖拖拉拉的;
}
如果(enemycount==0){
警惕(“做得好”);
//确认(“恭喜\n关卡完成!!!\n再次播放”)位置。重新加载();
}

我想做的是,当没有更多的敌人时,我想添加一个类似于以下内容的if语句:
if(enemycount==0){confirm('combly You Win\n Play?')window.location.reload();}

使用
setInterval
定期检查页面重新加载条件

enemycount=10;
设置间隔(()=>{

如果(enemycountenemycount值在全局范围内。请更改此值。enemycount==>enemycount

您需要检查正在触发的确认提示的响应

理想情况下,你需要这样做

var enemycount = 5;

    document.getElementById('counter').addEventListener('click',function(){
        enemycount--;
      die();
    })

    function die() {
      if(enemycount === 0){
        doContinue();
      }
    }

    function doContinue() {
        var resp = confirm('Congratulations You Win \n Play Again?') 
      if (resp) {
        window.location.reload();
      } else {
        //stay in the same window
      }

    }

Enemicount不是敌人的财产,它在窗口范围内。所以只需在您的骰子()中执行Enemicount--并检查同一函数内的敌方计数是否为0,以及reload@karthick如此接近于这样的东西?是的。但是改变这个。enemycount-->=>enemycount-->@karthigh谢谢你的帮助,弹出窗口可以工作。现在我只是在让游戏识别与重新加载相关的任何内容时遇到了问题。这是我在if状态中的设置ent了解我要为重新加载执行的操作。确认('Congreats\n Level Complete!!!\n Play Agree')窗口。重新加载();我不明白“游戏识别与重新加载相关的任何内容”是什么意思。是否要保留游戏状态,使EnemCount为0?