简体   繁体   English

画布游戏中的Js对象未捕获TypeError:无法读取未定义的属性'x'

[英]Js object in a canvas game Uncaught TypeError: Cannot read property 'x' of undefined

I am making a canvas game, which draws an object on the canvas. 我正在做一个画布游戏,该游戏在画布上绘制了一个对象。 But it keeps giving me an error x is undefined. 但是它一直给我一个错误x是不确定的。 It was working before but, when I added the star functions it broke. 它以前曾经工作过,但是当我添加star功能时,它坏了。 Any help as I am at a loss. 任何帮助,我都很茫然。

  var spaceShip = {
            position: {
                x: canvas.width / 2, 
                y: canvas.height - 20
            },
            size: {
                width: 50, 
                height: 12
            }, 
            Velocity: {
                x: 20
            }, 

            drawSpaceShip: function(){ // Draw Spaceship Object
                ctx.beginPath();
                ctx.fillStyle = "blue";
                ctx.fillRect(this.position.x, this.position.y, this.size.width, this.size.height);
                ctx.fillRect(this.position.x + 15, this.position.y, this.size.width - 30, this.size.height / 2 - 12);
                ctx.fillRect(this.position.x + 22.5, this.position.y, this.size.width - 45, this.size.height / 2 - 15);
                ctx.closePath();
                ctx.fill();
               requestAnimationFrame(this.drawSpaceShip);
            }// End drawShip function 
        };// End spaceShip Object

        function Star(x, y, rad, velocity, fill){
            this.x = Math.floor(Math.random() * 599);//this create a random number between 0 and 599 on the x axis
            this.y = 0;
            this.rad = Math.floor((Math.random() * 30) + 1);//this create a random number between 10 and 30 for the radius
            this.velocity = 6;
            this.fill = fill 

            this.draw = function(){
                ctx.beginPath();
                ctx.fillStyle = this.fill;                         
                ctx.arc(this.x, this.y, this.rad, 0, Math.PI * 2, true);
                ctx.closePath();
                ctx.fill();
                this.y += this.velocity;
            }
        }


        function createMultipleStars(){
            for (var i = 0; i <= 4; i++)
                stars[i] = new Star(i * 50, 10, i, i, "rgba(255,215,0,0.6)");
        }
        createMultipleStars();

        function step() {
            ctx.clearRect(0,0,canvas.width, canvas.height);
            for (var i = 0; i<= 4; i++)
                stars[i].draw();
            requestAnimationFrame(step);
        }

       spaceShip.drawSpaceShip();
       step();

You are loosing spaceShip object context when detaching this.drawSpaceShip method and passing it to requestAnimationFrame function. 当分离this.drawSpaceShip方法并将其传递给requestAnimationFrame函数时,您将失去spaceShip对象的上下文。 In this case drawSpaceShip is invoked with global object context ( this === window ). 在这种情况下,使用全局对象上下文( this === window )调用drawSpaceShip You can bind context explicitly with Function.prototype.bind method: 您可以使用Function.prototype.bind方法显式绑定上下文:

requestAnimationFrame(this.drawSpaceShip.bind(this));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 js -Uncaught TypeError: Cannot read property 'x' of undefined at object (ball - js -Uncaught TypeError: Cannot read property 'x' of undefined at object (ball Uncaught TypeError:无法读取未定义的属性“ canvas”-Javascript对象 - Uncaught TypeError: Cannot read property 'canvas' of undefined - Javascript Object 未捕获的TypeError:无法读取未定义的common.js的属性&#39;x&#39; - Uncaught TypeError: Cannot read property 'x' of undefined common.js 未捕获的TypeError:无法读取未定义的Morris.js的属性&#39;x&#39; - Uncaught TypeError: Cannot read property 'x' of undefined Morris.js 未捕获的TypeError:无法读取未定义的属性x - Uncaught TypeError: Cannot read property x of undefined 未捕获的TypeError:无法读取未定义的属性“ x” - Uncaught TypeError: Cannot read property 'x' of undefined Uncaught typeError:无法读取未定义的属性-对象 - Uncaught typeError: Cannot read property of undefined - object 未捕获的类型错误:无法读取未定义的属性“对象” - Uncaught TypeError: Cannot read property 'object' of undefined 未捕获的类型错误:无法读取 JavaScript Memory 游戏中未定义的属性“0” - Uncaught TypeError: Cannot read property '0' of undefined in JavaScript Memory game 未捕获的类型错误:无法读取网球比赛中未定义的属性“宽度” - Uncaught TypeError: Cannot read property 'width' of undefined inside tennis game
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM