我正在使用javascript制作太空入侵者游戏。现在我试图调整它,所以它在我的太空入侵者游戏中有2个等级。所以让我说到这一点,在控制台中它说:
未捕获的ReferenceError:未定义游戏级别
它还告诉我它在第397行,这是我的while循环的结束,我给它一个条件。我试了几个小时试图解决这个问题,但根本不能。我需要你的帮助!谢谢!这是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Space Invaders</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style>
canvas {
position: absolute;
top: 0px;
left: 0px;
background: transparent;
}
#backgroundCanvas {
background-color: black;
}
</style>
</head>
<body>
<canvas id="backgroundCanvas" width="550" height="600"></canvas>
<canvas id="playerCanvas" width="550" height="600"></canvas>
<canvas id="enemiesCanvas" width="550" height="600"></canvas>
<script>
(function() {
do{
$(document).ready(function() {
var gameWonSoundCount = 0;
var gameEndSoundCount = 0;
var game = {};
var gamelevel = 1;
game.stars = [];
game.width = 550;
game.height = 600;
game.images = [];
game.doneImages = 0;
game.requiredImages = 0;
game.keys = [];
game.projectiles = [];
game.enemies = [];
game.count = 0;
game.division = 48;
game.left = false;
game.enemySpeed = 3;
game.fullShootTimer = 10;
game.shootTimer = game.fullShootTimer;
game.gameOver = false;
game.gameWon = false;
game.gameWon2 = false;
game.moving = false;
game.explodeSound = new Audio("Explosion.wav");
game.gameWonSound = new Audio("GameWon.mp3");
game.gameOverSound = new Audio("GameOver.mp3");
game.gameEndSound = new Audio("GameWon.mp3");
game.shootSound = new Audio("Shoot.wav");
game.contextBackground = document.getElementById("backgroundCanvas").getContext('2d');
game.contextPlayer = document.getElementById("playerCanvas").getContext('2d');
game.contextEnemies = document.getElementById("enemiesCanvas").getContext('2d');
game.player = {
x: game.width / 2 - 50,
y: game.height - 103,
width: 80,
height: 100,
speed: 3,
rendered: false
}
$(document).keydown(function(e) {
game.keys[e.keyCode ? e.keyCode : e.which] = true;
})
$(document).keyup(function(e) {
delete game.keys[e.keyCode ? e.keyCode : e.which];
})
/*
up -38
down-40
left -37
right-39
w-87
a-65
s-83
d-68
space-32
*/
function addBullet() {
game.projectiles.push({
x: game.player.x,
y: game.player.y,
size: 20,
speed: 3,
image: 2
})
}
function init() {
for (i = 0; i < 600; i++) {
game.stars.push({
x: Math.floor(Math.random() * game.width),
y: Math.floor(Math.random() * game.height),
size: Math.random() * 5
})
}
for (y = 0; y < 5; y++) {
for (x = 0; x < 5; x++) {
game.enemies.push({
x: (x * 70) + (70 * x) + 10,
y: (y * 70) + (10 * y) + 40,
width: 70,
height: 70,
image: 1,
dead: false,
deadTime: 20
})
}
}
loop();
setTimeout(function() {
game.moving = true;
}, 5000)
}
function addStars(num) {
for (i = 0; i < num; i++) {
game.stars.push({
x: Math.floor(Math.random() * game.width),
y: game.height + 10,
size: Math.random() * 5
})
}
}
function update() {
addStars(1);
game.count++;
if (game.shootTimer > 0) {
game.shootTimer--;
}
for (i in game.stars) {
if (game.stars[i].y <= -5) {
game.stars.splice(i, 1);
}
game.stars[i].y--;
}
if (game.keys[37] || game.keys[65]) {
if (!game.gameOver) {
if (game.player.x >= 0) {
game.player.x -= game.player.speed;
game.player.rendered = false;
}
}
}
if (game.keys[39] || game.keys[68]) {
if (!game.gameOver) {
if (game.player.x <= 500 - 50) {
game.player.x += game.player.speed;
game.player.rendered = false;
}
}
}
if (game.count % game.division == 0) {
game.left = !game.left;
}
for (i in game.enemies) {
if (!game.moving) {
if (game.left) {
game.enemies[i].x -= game.enemySpeed;
} else {
game.enemies[i].x += game.enemySpeed;
}
}
if (game.moving) {
game.enemies[i].y++;
}
if (game.enemies[i].y >= 670) {
game.gameOver = true;
}
}
for (i in game.projectiles) {
game.projectiles[i].y -= 3;
if (game.projectiles[i].y <= -10) {
game.projectiles.splice(i, 1)
}
}
if (game.keys[32] && game.shootTimer <= 0) {
addBullet();
game.shootSound.play();
game.shootTimer = game.fullShootTimer
}
for (m in game.enemies) {
for (p in game.projectiles) {
if (collision(game.enemies[m], game.projectiles[p])) {
game.explodeSound.play();
game.enemies[m].dead = true;
game.enemies[m].image = 3;
game.projectiles.splice(p, 1)
}
}
}
for (i in game.enemies) {
if (game.enemies[i].dead) {
game.enemies[i].deadTime--;
}
if (game.enemies[i].dead && game.enemies[i].deadTime <= 0) {
game.contextEnemies.clearRect(game.enemies[i].x, game.enemies[i].y, game.enemies[i].size, game.enemies[i].size);
game.enemies.splice(i, 1);
}
}
if (game.enemies.length <= 0) {
gamelevel = +1;
if (gamelevel >= 3) {
game.gameWon = true;
}
if (gamelevel== 2) {
game.division = 24;// Makes the enemies go left and right faster
game.gameWon2 = true;
}
}
}
function render() {
game.contextBackground.clearRect(0, 0, game.width, game.height)
game.contextBackground.fillStyle = "white";
for (i in game.stars) {
var star = game.stars[i];
game.contextBackground.fillRect(star.x, star.y, star.size, star.size);
}
if (!game.player.rendered) {
game.contextPlayer.clearRect(0, 0, game.width, game.height);
game.contextPlayer.drawImage(game.images[0], game.player.x, game.player.y, game.player.width, game.player.height);
game.player.rendered = true;
}
game.contextBackground.clearRect(0, 0, game.width, game.height);
game.contextEnemies.clearRect(0, 0, game.width, game.height);
for (i in game.enemies) {
var enemy = game.enemies[i];
game.contextEnemies.drawImage(game.images[enemy.image], enemy.x, enemy.y, enemy.width, enemy.height);
}
for (i in game.projectiles) {
var proj = game.projectiles[i];
game.contextEnemies.drawImage(game.images[proj.image], proj.x, proj.y, proj.size, proj.size);
}
if (game.gameOver) {
var gradient = game.contextPlayer.createLinearGradient(0, 0, game.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "white");
gradient.addColorStop("1.0", "black");
// Fill with gradient
game.contextPlayer.fillStyle = gradient;
game.contextPlayer.font = "bold 50px monaco"
game.contextPlayer.fillText("Game Over!", game.width / 2 - 130, game.height / 2 - 25);
if (gameEndSoundCount < 2) {
gameEndSoundCount += 1;
game.gameOverSound.play();
}
}
if (game.gameWon) {
var gradient = game.contextPlayer.createLinearGradient(0, 0, game.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "white");
gradient.addColorStop("1.0", "black");
// Fill with gradient
game.contextPlayer.fillStyle = gradient;
game.contextPlayer.font = "bold 50px monaco"
game.contextPlayer.fillText("Game Won!", game.width / 2 - 130, game.height / 2 - 25);
if (gameWonSoundCount < 2) {
gameWonSoundCount += 1;
game.gameWonSound.play();
}
}
if (game.gameWon2) {
var gradient2 = game.contextPlayer.createLinearGradient(0, 0, game.width, 0);
gradient2.addColorStop("0", "magenta");
gradient2.addColorStop("0.5", "white");
gradient2.addColorStop("1.0", "black");
// Fill with gradient
game.contextPlayer.fillStyle = gradient2;
game.contextPlayer.font = "bold 50px monaco"
game.contextPlayer.fillText("Level 2", game.width / 2 - 130, game.height / 2 - 25);
if (gameWonSoundCount < 2) {
gameWonSoundCount += 1;
game.gameWonSound.play();
}
}
}
function loop() {
requestAnimFrame(function() {
loop();
});
update();
render();
}
function initImages(paths) {
game.requiredImages = paths.length;
for (i in paths) {
var img = new Image;
img.src = paths[i];
game.images[i] = img;
game.images[i].onload = function() {
game.doneImages++;
}
}
}
function collision(first, second) {
return !(first.x > second.x + second.width ||
first.x + first.width < second.x ||
first.y > second.y + second.height ||
first.y + first.height < second.y);
}
function checkImages() {
if (game.doneImages >= game.requiredImages) {
init();
} else {
setTimeout(function() {
checkImages();
}, 1)
}
}
game.contextBackground.font = "bold 50px monaco"
game.contextBackground.fillStyle = "white";
game.contextBackground.fillText("loading", game.width / 2 - 100, game.height / 2 - 25)
initImages(["player.gif", "enemy.png", "bullet.png", "explosion.gif"])
checkImages();
});
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
}while(gamelevel>=3)
})();
</script>
</body>
</html>
答案 0 :(得分:1)
如果在gamelevel
循环中宣布do..while
,则while
部分无法访问该var gamelevel = 1;
do {
...
。你需要将它移到外面:
$(document).ready()
我很好奇,为什么你有一个do..while
{{1}}。这似乎不对。
答案 1 :(得分:0)
您应该在执行
之前定义gameLevel varvar gamelevel = 1;
do{
$(document).ready(function() {
var gameWonSoundCount = 0;
var gameEndSoundCount = 0;
var game = {};
...
在当前版本中,变量不在while()context
中