我正在嘗試制作一個 javascript 游戲,但是在我導入 js 文件(car.js)后,我的 html 畫布上沒有顯示任何內(nèi)容,在我導入car.js之前一切都顯示了。我目前有一個名為 cargame 的文件夾,它包含 3 個文件,car.js,firstgame.html和index.js。我正在提供以下文件的內(nèi)容。firstgame.html<!DOCTYPE html><html><head><title>Web Page Design</title><style>#gamescreen{ border: 1px solid black;}#bgcolor{ background-color: #b4b4b4;}</style></head><body id="bgcolor"> <div align = "center" style="margin-top: 5%"><canvas id = "gamescreen" width = "500px" height = "600px"></canvas></div> <script src="index.js"></script></body></html>index.jsimport car from "./car";let canvas = document.getElementById("gamescreen");let ctx = canvas.getContext('2d');const GAME_WIDTH = 500;const GAME_HEIGHT = 600;ctx.fillStyle = 'black';ctx.fillRect(0, 0, canvas.width/3, canvas.height)ctx.fillRect((canvas.width/3 + canvas.width/3), 0, canvas.width/3, canvas.height)ctx.fillStyle = '#2a2a2a';ctx.fillRect(canvas.width/3, 0, canvas.width/3, canvas.height)ctx.clearRect(0,0,500,600)let Car = new car(GAME_WIDTH, GAME_HEIGHT);Car.draw(ctx);汽車.jsexport default class car{ constructor(gamewidth, gameheight){ this.width = 100; this.height = 100; this.position = { x : gamewidth/2 - this.width/2, y : gameheight - this.height - 10, }; } draw(ctx){ ctx.fillStyle = '#ff6969'; ctx.fillRect(this.position.x, this.position.y, this.width, this.height); }}我的日志中出現(xiàn)以下錯誤firstgame.html:1 Access to script at 'file:///C:/MY%20DATA/java%20projects/cargame/index.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.錯誤 2firstgame.html:19 GET file:///C:/MY%20DATA/java%20projects/cargame/index.js net::ERR_FAILED
導入 js 文件后,我的畫布上沒有任何顯示
幕布斯7119047
2022-10-13 10:41:07