[英]Brackets p5.js “Uncaught TypeError: Cannot read property 'offscreen' of undefined”
I am learning about Neural Networks(and learning JS at the same time) at the moment and wanted to start with a little game in Javascript to implement my learned stuff. 我目前正在学习神经网络(同时学习JS),并希望从Javascript的一个小游戏开始以实现我学到的东西。 So first things first, create a game, shouldn't be too hard right?
所以首先要做的是创建游戏,难道不应该太难了吧? Well.. Looks like I am too blind to find the mistake, so I would be really glad if someone could help me.
好吧..看来我太盲目找不到错误,所以如果有人可以帮助我,我将非常高兴。
I am getting the following error message: "Uncaught TypeError: Cannot read property 'offscreen' of undefined". 我收到以下错误消息:“未捕获的TypeError:无法读取未定义的属性'offscreen'”。
It occurs when the player dies. 它在玩家死亡时发生。 I guess it has to do with the array being cleared when "resetting" and maybe the for-loop then still tries to delete the offscreen pipe, but it cant, as the array is empty or something like this.
我猜想这与“重置”时清除数组有关,也许for循环然后仍尝试删除屏幕外管道,但它不能,因为数组为空或类似的东西。 Dont know how to fix that tho..
不知道如何解决这个问题。
https://thumbs.gfycat.com/WanDifficultAnaconda-size_restricted.gif https://thumbs.gfycat.com/WanDifficultAnaconda-size_restricted.gif
The Game is just an easy "objects come flying to you and you gotta jump over them". 游戏只是一个简单的“物体飞向您而您必须跳过它们”。
Here's the whole code, it really isnt much: 这是整个代码,实际上并不多:
sketch.js sketch.js
let obstacle;
let player;
let obstacles = [];
let score = 0;
let highscore = 0;
function setup() {
createCanvas(600, 600);
obstacle = new Obstacle();
player = new Player();
}
function draw() {
background(0, 128, 128);
for(let i = obstacles.length-1; i >= 0; i--) {
if(obstacles[i].hits(player)) {
reset();
}
if(obstacles[i].offscreen()) {
obstacles.splice(i, 1);
}
obstacles[i].show();
obstacles[i].update();
}
player.show();
player.update();
score++;
currentScore();
newhighscore();
if(frameCount % 100 == 0) {
obstacles.push(new Obstacle());
}
}
function keyPressed() {
if(keyCode == 87 && player.y + player.h == height) {
player.jump();
}
}
function reset() {
if(score > highscore) {
highscore = score;
}
obstacles = [];
score = 0;
player = new Player();
}
function currentScore() {
strokeWeight(0);
stroke(102,205,170);
fill(14,47,68);
textSize(24);
text(score, 10, 30);
}
function newhighscore() {
strokeWeight(0);
stroke(102,205,170);
fill(14,47,68);
textSize(16);
text(highscore, 11, 48);
}
player.js player.js
class Player {
constructor() {
this.x = 100;
this.h = 30;
this.w = this.h;
this.y = height - this.h;
this.gravity = 0.5;
this.lift = -9;
this.velocity = 0;
}
show() {
fill(0);
rect(this.x, this.y, this.w, this.h);
}
update() {
this.velocity += this.gravity;
this.y += this.velocity;
if(this.y + this.h > height) {
this.y = height - this.h;
this.velocity = 0;
}
}
jump() {
this.velocity += this.lift;
}
}
obstacle.js barrier.js
class Obstacle {
constructor() {
this.x = width;
this.h = random(30, 50);
this.w = this.h;
this.y = height-this.h;
this.speed = 5;
}
show() {
noStroke();
fill(255, 115, 115);
rect(this.x, this.y, this.w, this.h);
}
update() {
this.x -= this.speed;
}
offscreen() {
if(this.x < -width) {
return true;
} else {
false;
}
}
hits(player) {
if(player.x <= this.x + this.w && player.x + player.w >= this.x) {
if(player.y + player.h >= this.y) {
return true;
} else {
return false;
}
}
}
}
Yeah you had the reason correct, when reset()
is called, it changes to obstacles = []
but the 是的,您有一个正确的理由,当调用
reset()
时,它会变为obstacles = []
但是
for(let i = obstacles.length-1; i >= 0; i--) {
if(obstacles[i].hits(player)) {
reset();
...*Continued*
}
in your draw function still continues. 在您的绘制功能仍然继续。 So you just had to add break;
因此,您只需要添加中断即可; in the hit condition after reset.
重置后处于命中状态。 See in the link I've uploaded it works completely fine.
请参阅我上传的链接,它可以正常工作。
for(let i = obstacles.length-1; i >= 0; i--) {
if(obstacles[i].hits(player)) {
reset();
break;
...*Continued*
}
When you add break, the for loop terminates and moves down to the functions that follow. 添加break时,for循环终止,并移至随后的函数。 Since
obstacles = []
is empty, undefined
error is not shown as empty indexes are not accessed. 由于
obstacles = []
为空,因此未显示undefined
错误,因为未访问空索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.