首页 > 解决方案 > 未捕获的类型错误:无法读取未定义的属性“显示”

问题描述

我正在使用 Java 脚本制作一个简单的砖块破坏游戏,我的控制台在画布上显示块时向我显示错误,它们被绘制到画布上但所有其他对象都不起作用并且控制台正在显示

index.js:173 未捕获类型错误:无法在 index.js:177 处读取更新时未定义的属性“显示”(index.js:173)

如果您从第 172 和 173 行注释掉,这是一个 for 循环,告诉画布在其上绘制它们,一切正常。

还有一件事:canvasRendering...rundedRectangle 是一个绘制圆角矩形的函数,有人请找到解决方案!

CanvasRenderingContext2D.prototype.roundedRectangle = function(x, y, width, height, rounded) {
    const radiansInCircle = 2 * Math.PI
    const halfRadians = (2 * Math.PI)/2
    const quarterRadians = (2 * Math.PI)/4  
    
    // top left arc
    this.arc(rounded + x, rounded + y, rounded, -quarterRadians, halfRadians, true)
    
    // line from top left to bottom left
    this.lineTo(x, y + height - rounded)
  
    // bottom left arc  
    this.arc(rounded + x, height - rounded + y, rounded, halfRadians, quarterRadians, true)  
    
    // line from bottom left to bottom right
    this.lineTo(x + width - rounded, y + height)
  
    // bottom right arc
    this.arc(x + width - rounded, y + height - rounded, rounded, quarterRadians, 0, true)  
    
    // line from bottom right to top right
    this.lineTo(x + width, y + rounded)  
  
    // top right arc
    this.arc(x + width - rounded, y + rounded, rounded, 0, -quarterRadians, true)  
    
    // line from top right to top left
    this.lineTo(x + rounded, y)  
}

var canvas= document.getElementById("gameCanvas");
var ctx = canvas.getContext("2d");

function Player(x,y,w,h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.show = function(){
        ctx.beginPath();
        ctx.rect(this.x, this.y, this.w, this.h);
        ctx.fillStyle = "#ffff";
        ctx.fill();
        ctx.closePath();
    }
    this.move = function(speed){
        this.x += speed;
    }
}

function Ball(x,y,r){
    this.x = x;
    this.y = y;
    this.r = r;
    this.show = function(){
        ctx.beginPath();
        ctx.arc(this.x,this.y,this.r,0,2* Math.PI);
        ctx.fillStyle = "tomato";
        ctx.fill();
        ctx.closePath();
    }
    this.move= function(speedX,speedY){
        this.show();
        this.speed = 2;
        this.x += speedX;
        this.y += speedY;
    }

}

function Block(x,y,w,h){
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    this.show= function(color){
        ctx.beginPath();
        ctx.roundedRectangle(this.x,this.y,this.w,this.h,7);
        ctx.fillStyle = color;
        ctx.fill();
        ctx.closePath();
    };
};

var player = new Player(canvas.width/2-50,canvas.height*0.95,100,20);
var ball = new Ball(canvas.width/2-5, player.y-20,15);
var rigthPressed = false;
var leftPressed = false;

var blocks = [];
var rowCount = 5;
var columnCount = 6;
var noInRow = 6;
var blockCount = (rowCount*columnCount)+1;
var blockRow = 0;
var blockCol = 0;

var ballSpeedX = 5;
var ballSpeedY = -10;
for(let i = 1; i < blockCount; i++){
    blocks.push(new Block(blockCol*60+25,blockRow*60+30,50,50)); 
    blockCol++;
    if(i % noInRow == 0){
        blockRow++;
        blockCol = 0;
    }
}


window.addEventListener("keydown", function(e){
    if(e.keyCode == 39){
        rigthPressed = true;
    }
    if(e.keyCode == 37){
        leftPressed = true;
    }
});
window.addEventListener("keyup", function(e){
    if(e.keyCode == 39){
        rigthPressed = false;
    }
    if(e.keyCode == 37){
        leftPressed = false;
    }
});

function objMovement(){
    if(rigthPressed){
        player.move(5);
        if (player.x > canvas.width-player.w){
            player.x = canvas.width-player.w;
        }
    }
    if(leftPressed){
        player.move(-5);
        if(player.x < 0){
            player.x = 0;
        }
    }

    if(ball.x > canvas.width-ball.r || ball.x < 0+ball.r){
        ballSpeedX = -ballSpeedX;
    }
    if (ball.y > canvas.height-ball.r || ball.y < 0+ball.r){
        ballSpeedY = -ballSpeedY;
    }
    if(ball.x<player.x+player.w &&ball.x>player.x && ball.y>player.y){
        ballSpeedY = -ballSpeedY;
        ballSpeedX= ballSpeedX;
    }
    function Bump(){
        if (ball.x>player.x && ball.x<player.x+player.w/2){
            if (ball.y >= player.y){
                ballSpeedX = -5;
            }
        }
        if(ball.x>player.x+player.w/2 && ball.x<player.x+player.w){
            if(ball.y >= player.y){
                ballSpeedX = 5;
            }
        }
    }   
    //Bump();
}

function update(){
    ctx.clearRect(0,0,canvas.width,canvas.height);
    ball.show();
    ball.move(ballSpeedX,ballSpeedY);
    player.show();
    objMovement();
    for(let i=0;i<blockCount;i++){
        blocks[i].show("violet");    
    }
    window.requestAnimationFrame(update);
}
update();
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        #body{
            background-color: rgb(31, 30, 30);
        }
        #gameCanvas{
            border: 15px solid rgb(44, 44, 44);
            border-radius: 20px;
            background-color:rgb(19, 18, 18);
            margin: 250px;
        }
    </style>
</head>
<body id="body">
    <canvas id="gameCanvas" width=400 height=800></canvas>
    <script type="text/javascript" src="./index.js"></script>
    
</body>
</html>

标签: javascripthtmlcanvashtml5-canvas

解决方案


当您创建时,blocks您从 1 开始

for (let i = 1; i < blockCount; i++) {
    blocks.push(new Block(blockCol * 60 + 25, blockRow * 60 + 30, 50, 50));
    ...

因此,当您更新时,您需要考虑

function update() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ball.show();
    ball.move(ballSpeedX, ballSpeedY);
    player.show();
    objMovement();
    for (let i = 0; i < blockCount -1; i++) { // or for (let i = 0; i < blocks.length; i++) {
        blocks[i].show("violet");
    }
    window.requestAnimationFrame(update);
}

推荐阅读