简体   繁体   中英

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. It was working before but, when I added the star functions it broke. 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. In this case drawSpaceShip is invoked with global object context ( this === window ). You can bind context explicitly with Function.prototype.bind method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM