gpt4 book ai didi

javascript - TypeError : this. _grid[x] 未定义

转载 作者:太空宇宙 更新时间:2023-11-04 16:28:16 25 4
gpt4 key购买 nike

我正在尝试从tutorial构建一个贪吃蛇游戏。在 JavaScript 和 Canvas 中。此时我在这一行收到一个错误(TypeError:y未定义):

return this._grid[x][y];

它是 var 网格对象的一部分。如果有人能给我指明正确的方向,这样我就可以完成游戏,我将非常感激!谢谢。

<script>
//Constants
var COLS = 26, ROWS = 26;
// IDs
var EMPTY=0, SNAKE = 1, FRUIT = 2;
// Directions
var LEFT = 0, UP = 1, RIGHT = 2, DOWN = 3;
// KeyCodes
var KEY_LEFT=37, KEY_UP=38, KEY_RIGHT=39, KEY_DOWN=40;

var grid = {
width: null,
height: null,
_grid: null,

init: function(d,c,r){
this.width = c;
this.height = r;

this._grid = [];
for(var x=0; x<c; x++){
this._grid.push([]);
for(var y=0; y < r; y++){
this._grid[x].push(d);
}
}
},

set: function(val,x,y){
this._grid[x][y] = val;
},

get: function(x,y){
return this._grid[x][y];
}
}

var snake = {
direction: null,
last: null,
_queue: null,

init: function(d,x,y){
this.direction = d;
this._queue = [];
this.insert(x,y);
},

insert: function(x,y){
this._queue.unshift({x:x, y:y});
this.last = this._queue[0];
},

remove: function(){
return this._queue.pop();
}
}

function setFood() {
var empty = [];
for (var x = 0; x < grid.width; x++) {
for (var y = 0; y < grid.height; y++) {
if (grid.get(x, y) === EMPTY) {
empty.push({x:x, y:y});
}
}
}
var randpos = empty[Math.floor(Math.random()*empty.length)];
grid.set(FRUIT, randpos.x, randpos.y);
}

//Game objects
var canvas, ctx, keystate, frames, score;

function main() {
canvas = document.createElement("canvas");
canvas.width = COLS*20;
canvas.height = ROWS*20;
ctx = canvas.getContext("2d");
document.body.appendChild(canvas);

ctx.font = "12px Helvetica"

frames = 0;
keystate = {};
document.addEventListener("keydown", function(evt) {
keystate[evt.keyCode] = true;
});
document.addEventListener("keyup", function(evt) {
delete keystate[evt.keyCode];
});

init();
loop();
}

function init(){
grid.init(EMPTY, COLS, ROWS);

var sp = {x:Math.floor(COLS/2), y:ROWS-1};
snake.init(UP, sp.x, sp.y);
grid.set(SNAKE, sp.x, sp.y);

setFood();
}

function loop(){
update();
draw();

window.requestAnimationFrame(loop, canvas);
}

function update() {
frames++;

if (keystate[KEY_LEFT] && snake.direction !== RIGHT)
snake.direction = LEFT;
if (keystate[KEY_UP] && snake.direction !== DOWN)
snake.direction = UP;
if (keystate[KEY_RIGHT] && snake.direction !== LEFT)
snake.direction = RIGHT;
if (keystate[KEY_DOWN] && snake.direction !== UP)
snake.direction = DOWN;

if (frames%5 === 0) {
var nx = snake.last.x;
var ny = snake.last.y;

switch (snake.direction) {
case LEFT:
nx--;
break;
case UP:
ny--;
break;
case RIGHT:
nx++;
break;
case DOWN:
ny++;
break;
}

if (grid.get(nx, ny) === FRUIT) {
var tail = {x:nx, y:ny};
score++;
setFood();
} else {
var tail = snake.remove();
grid.set(EMPTY, tail.x, tail.y);
tail.x = nx;
tail.y = ny;
}




grid.set(SNAKE, tail.x, tail.y);

snake.insert(tail.x, tail.y);
}

if (0 > nx || nx > grid.width-1 ||
0 > ny || ny > grid.height-1
|| grid.get(nx, ny) === SNAKE
) {
return init();
}
}

function draw() {
var tw = canvas.width/grid.width;
var th = canvas.height/grid.height;

for (var x = 0; x < grid.width; x++) {
for (var y = 0; y < grid.height; y++) {
switch (grid.get(x, y)) {
case EMPTY:
ctx.fillStyle = "#fff";
break;
case SNAKE:
ctx.fillStyle = "#0ff";
break;
case FRUIT:
ctx.fillStyle = "#f00";
break;
}
ctx.fillRect(x*tw, y*th, tw, th);
}
}
ctx.fillStyle = "#000";
ctx.fillText("SCORE: " + score, 10, canvas.height-10);
};

main();

</script>

最佳答案

如果您传递的数据不能保证在正确的范围内,您需要审查或捕获超出范围的值。

如果您认为数据不需要审查,您需要找到索引未定义数组项的错误值 x 的来源。

对于调试,可以在 getter 中添加一个断点,然后使用调试器找出 x 的哪个值不正确并引发错误,或者将调试器设置为“出错时中断”并检查为您提供的 x 值错误的引用。

将 get 函数更改为

    get: function(x,y){
try{
return this._grid[x][y];
} catch(e){
console.error("access to _grid["+x+ "][" + y +"] failed");
throw e;
}
}

这样你就可以看到错误抛出时的x值是多少。

找出原因并解决问题后,您可以删除 try catch

关于javascript - TypeError : this. _grid[x] 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40038269/

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