JavaScript可以';不要看以前使用过的礼节
我想做一个简单的游戏,更确切地说是蛇。无论如何,我在控制台中遇到了一个错误,它无法读取JavaScript可以';不要看以前使用过的礼节,javascript,Javascript,我想做一个简单的游戏,更确切地说是蛇。无论如何,我在控制台中遇到了一个错误,它无法读取snake[0].xproperty(从snake[score]行读取)。x=snake[0].x已被精确使用 function scoreManager(){ if(snake[0].x==food.x && snake[0].y==food.y){ score++; snake[score].x = snake[0].x; snake[
snake[0].x
property(从snake[score]行读取)。x=snake[0].x代码>已被精确使用
function scoreManager(){
if(snake[0].x==food.x && snake[0].y==food.y){
score++;
snake[score].x = snake[0].x;
snake[score].y = snake[0].y;
food = {
x:Math.floor(Math.random()*30+1)*cell,
y:Math.floor(Math.random()*29+1)*cell
};
}
}
它在if语句中读得很好(我知道这一点,因为如果该语句为真,分数会增加)
我得到的确切错误是:
> game_engine.js:86 Uncaught TypeError: Cannot set property 'x' of
> undefined
> at scoreManager (game_engine.js:86)
> at draw (game_engine.js:110) scoreManager @ game_engine.js:86 draw @ game_engine.js:110 setInterval (async) (anonymous) @
> game_engine.js:127
为什么会发生这种情况?
哦,而且出于某种原因,新的食物坐标总是一样的
这是完整的代码
const cvs = document.getElementById("gameArea");
const ctx = cvs.getContext("2d");
var playGround = new Image();
var foodSprite = new Image();
var sandSprite = new Image();
var snakeSprite = new Image();
var tailSprite = new Image();
playGround.src = './Textures/play_area.png';
foodSprite.src = './Textures/Food.png';
sandSprite.src = './Textures/sand.png';
snakeSprite.src = './Textures/snake_head.png';
tailSprite.src = './Textures/Snake_body.png';
//variables
const cell = 32;
let score = 1;
let direction;
//objects
let snake = [];
snake[0] = { x: 1 * cell, y: 1 * cell };
let food = {
x:Math.floor(Math.random()*30+1)*cell,
y:Math.floor(Math.random()*29+1)*cell
};
//controls
document.addEventListener('keydown',directionGo);
function directionGo(event){
if(event.keyCode == 37 && direction != 'RIGHT'){
direction = 'LEFT';
} else
if(event.keyCode == 38 && direction != 'DOWN'){
direction = 'UP';
} else
if(event.keyCode == 39 && direction != 'LEFT'){
direction = 'RIGHT';
} else
if(event.keyCode == 40 && direction != 'UP'){
direction = 'DOWN';
}
}
function directionManager(){
if(direction=="LEFT"){
snake[0].x-=cell;
} else
if(direction=="RIGHT"){
snake[0].x+=cell;
} else
if(direction=="UP"){
snake[0].y-=cell;
} else
if(direction=="DOWN"){
snake[0].y+=cell;
}
}
//in case u mess up
function gameOver (){
for (let i = 1; i<snake.length; i++){
if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){
return true;
}
}
if(snake[0].x == 0*cell || snake[0].x == 32*cell || snake[0].y == 0*cell || snake[0].y == 31*cell){
return true;
}
}
function respawn (){
if(gameOver() == true){
snake = [];
snake[0] = { x: 1 * cell, y: 1 * cell };
score = 0;
direction='RIGHT';
}
}
//in case u a gud boy
function scoreManager(){
if(snake[0].x==food.x && snake[0].y==food.y){
score++;
snake[score].x = snake[0].x;
snake[score].y = snake[0].y;
food = {
x:Math.floor(Math.random()*30+1)*cell,
y:Math.floor(Math.random()*29+1)*cell
};
}
}
function tail(){
}
//Drawing function that manages the food , map and the player
function draw() {
ctx.drawImage(playGround, 0, 0); // ground
//drawing snake
ctx.drawImage(snakeSprite, snake[0].x, snake[0].y);
for(let i = 1; i < snake.length; i++){
ctx.drawImage(snakeSprite, snake[i].x, snake[i].y);
}
//direction o.o
directionManager();
scoreManager();
//food
ctx.drawImage(foodSprite,food.x,food.y);
//score
ctx.fillStyle='white';
ctx.font = '40px Changa One';
ctx.fillText(score,16*cell+5,1*cell+15);
//loops the animation
//window.requestAnimationFrame(draw);
respawn();
}
//draw();
let game = setInterval (draw,100);
const cvs=document.getElementById(“游戏区”);
const ctx=cvs.getContext(“2d”);
var=newimage();
var foodSprite=新图像();
var sandSprite=新图像();
var snakeSprite=新图像();
var tailSprite=新图像();
playerd.src='./Textures/play_area.png';
foodSprite.src='./Textures/Food.png';
sandSprite.src='./纹理/sand.png';
snakeSprite.src='./Textures/snake_head.png';
tailSprite.src='./Textures/Snake_body.png';
//变数
常量单元格=32;
让分数=1;
让方向;
//物体
让snake=[];
snake[0]={x:1*单元,y:1*单元};
让食物{
x:Math.floor(Math.random()*30+1)*单元格,
y:Math.floor(Math.random()*29+1)*单元格
};
//控制
文件。添加的监听器('keydown',directionGo);
函数方向Go(事件){
如果(event.keyCode==37&&direction!='RIGHT'){
方向='左';
}否则
如果(event.keyCode==38&&direction!='DOWN'){
方向=‘向上’;
}否则
如果(event.keyCode==39&&direction!='LEFT'){
方向='右';
}否则
如果(event.keyCode==40&&direction!='UP'){
方向=‘向下’;
}
}
函数方向管理器(){
如果(方向=“左”){
snake[0].x-=细胞;
}否则
如果(方向=“右”){
snake[0].x+=cell;
}否则
如果(方向=“向上”){
snake[0].y-=细胞;
}否则
如果(方向=“向下”){
snake[0].y+=cell;
}
}
//万一你搞砸了
函数gameOver(){
对于(让i=1;iifscore
是属性名使用snake['score']
。您需要在属性名周围放置'
,在有问题的行中放置一个断点,并检查调试器的“LOCALS”(可能因浏览器而异)部分以查看变量的状态。我希望看到一些东西(即snake
或score
)未初始化。您没有将变量赋回数组几何,分数只是一个变量。因为snake[score]
未定义…您需要说snake[score]={x:snake[0].x,y:snake[0].y};
-错误不是关于snake[0].x
是关于snake[score].x