Скобки p5.js "Uncaught TypeError: Невозможно прочитать свойство 'offscreen' из неопределенного" - PullRequest
0 голосов
/ 08 мая 2018

В данный момент я изучаю нейронные сети (и одновременно изучаю JS) и хотел начать с небольшой игры на Javascript, чтобы реализовать мои изученные материалы. Итак, обо всем по порядку, создайте игру, не должно быть слишком сложно, верно? Ну ... Похоже, я слишком слеп, чтобы найти ошибку, поэтому я был бы очень рад, если бы кто-нибудь мог мне помочь.

Я получаю следующее сообщение об ошибке: «Uncaught TypeError: Невозможно прочитать свойство 'offscreen' of undefined".

Происходит, когда игрок умирает. Я предполагаю, что это связано с очищаемым массивом при «сбросе», и, возможно, цикл for все еще пытается удалить неэкранный канал, но это невозможно, поскольку массив пуст или что-то в этом роде. Не знаю, как это исправить ..

https://thumbs.gfycat.com/WanDifficultAnaconda-size_restricted.gif

Игра - это просто "объекты летят к вам, и вы должны перепрыгнуть через них".

Вот весь код, это действительно немного:

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

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

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;
            }
        }
    }
}

1 Ответ

0 голосов
/ 11 мая 2018

Мое решение

Объяснение

Да, у вас была правильная причина, когда вызывается reset(), она меняется на obstacles = [], но

    for(let i = obstacles.length-1; i >= 0; i--) {
        if(obstacles[i].hits(player)) {
        reset();
        ...*Continued*
    }

в вашей функции рисования все еще продолжается. Так что вам просто нужно было добавить перерыв; в состоянии удара после сброса. Посмотрите на ссылку, которую я загрузил, она работает совершенно нормально.

    for(let i = obstacles.length-1; i >= 0; i--) {
        if(obstacles[i].hits(player)) {
        reset();
        break;
        ...*Continued*
    }

Когда вы добавляете break, цикл for завершается и перемещается к следующим функциям. Поскольку obstacles = [] пусто, ошибка undefined не отображается, поскольку к пустым индексам нет доступа.

...