gpt4 book ai didi

javascript - 如何添加一个计数器,稍后将用于 if 语句以执行重新加载

转载 作者:行者123 更新时间:2023-11-30 06:13:23 24 4
gpt4 key购买 nike

我不确定如何正确设置游戏,所以我在数敌人。如果我能数出敌人数量,我相信我可以设置我的代码,这样我就可以确认是否成功,这样我就可以重新加载页面并可能制造更多敌人

我认为我的问题是基于我的 enemycount 变量。我不确定如何倒数到零。我也相信我的问题可能与没有基于 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();
}

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

最佳答案

enemycount 值在全局范围内。改变 this.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
}

}

关于javascript - 如何添加一个计数器,稍后将用于 if 语句以执行重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57598704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com