[英]How do I add a Counter that would later be used for an if statement to perform a reload
I am unsure how to properly set up a game so I am counting enemies. 我不确定如何正确设置游戏,所以我在数敌人。 If I can count the enemies I believe I can set up my code so I can do a confirm that would make it so I can reload the page and potentially make more enemies 如果我可以算出敌人,我相信我可以设置代码,这样我就可以做一个确认,这样我就可以重新加载页面并有可能产生更多的敌人
I think my problem is based around my enemycount variable. 我认为我的问题是基于我的敌人计数变量。 I am unsure how to count down to get to zero. 我不确定如何倒数到零。 I also believe my problem might be related to not having a function based around enemycount. 我也相信我的问题可能与没有基于敌人计数的功能有关。
//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();
}
What I am looking to do is when the there are no more enemies I was looking to add an if statement similar to: if(enemycount === 0){confirm('Congratulations You Win \\n Play Again?') window.location.reload();}
我要做的是在没有更多敌人的情况下添加类似于以下内容的if语句: if(enemycount === 0){confirm('Congratulations You Win \\n Play Again?') window.location.reload();}
Use setInterval
which will periodically check page-reload condition 使用setInterval
,它将定期检查页面重新加载情况
enemycount = 10; setInterval(() => { if(enemycount <=0) location='https://example.com'; console.log('check', enemycount); enemycount--; }, 500)
The enemycount value is in global scope. 敌人计数值是全球范围内的。 Change this.enemycount ==> enemycount; 更改this.enemycount ==>敌人计数;
And you need to check the response of the confirm prompt that you are triggering. 您需要检查触发的确认提示的响应。
Ideally you need to so something like this 理想情况下,您需要这样的事情
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.