egoros7
@egoros7
всё ясно автору 12 лет

Ошибка Uncaught TypeError: ball.move is not a function?

Почему появляется эта ошибка, заранее спасибо, вот код
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>engine</title>
    </head>
    <body>
        <canvas width="500px" height="500" id="2d">
        </canvas>
        <script>
            var canvas = document.getElementById("2d");
            var ctx = canvas.getContext("2d");
            var cvs = {x: 500, y: 500};

            function draw_circle (linewidth, x, y, radius, colorofborder, coloroffill, fill) {
                ctx.lineWidth = linewidth;
                ctx.strokeStyle = colorofborder;
                ctx.fillStyle = coloroffill;
                ctx.beginPath();
                ctx.arc (x, y, radius, 0, Math.PI*2, false);
                if (fill === false) {ctx.stroke();}
                else if (fill === true) {ctx.fill();}
            };
            // -------------------------------------------------------------------------------
            var gravity = 1;
            var phys_objects = {};
            // -------------------------------------------------------------------------------
            function Prop (x, y, mass, radius) {
                this.x = x;
                this.y = y;
                this.speed = {x: 20, y: 20};
                this.mass = mass;
                this.radius = radius;
            };
            Prop.__proto__.move = function () {
                ctx.clearRect(0, 0, 500, 500);
                this.x =+ this.speed.x;
                this.y =+ this.speed.y;
                this.speed.x--;
                this.speed.y = this.mass;
                draw_circle(this.x, this.y, this.radius, "black", "red", true);
                draw_circle(this.x, this.y, this.radius, "black", "red", false);
            };
            Prop.__proto__.check_collision = function () {
                if (this.speed.x != 0 || this.speed.y != 0) {
                if (this.x < this.radius || this.x > cvs.x-this.radius) {
                    this.speed.x = (-this.speed.x)/2;
                }
                else if (this.y < this.radius || this.y > cvs.y+this.radius) {
                    this.speed.y = (-this.speed.y)/2;
                } } else {return;}
            };

            // Start test!
            var ball = new Prop (50, 50, 10, 25);
            var time = setInterval(function() {
                ball.move();
                ball.check_collision();}, 1000);

        </script>
    </body>
</html>
  • Вопрос задан
  • 30 просмотров
Решения вопроса 1
bingo347
@bingo347 Куратор тега JavaScript
Crazy on performance...
Prop.__proto__.move поменять на Prop.prototype.move
Prop.__proto__.check_collision поменять на Prop.prototype.check_collision

Получше изучите прототипы, и чем prototype отличается от __proto__
Ну и обратите внимание на es6 class, позволяющий работать с прототипами максимально просто и правильно
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы