JavaScript上的导入/导出命令有问题
我的代码有问题。据我所见,它应该起作用,它对其他人起作用,但对我不起作用。如果有人能帮忙的话,我很难确定这个问题 我试过用谷歌搜索类似的问题,但没有结果。从YouTube视频上观看,但他对此没有问题。这是我的密码JavaScript上的导入/导出命令有问题,javascript,html,Javascript,Html,我的代码有问题。据我所见,它应该起作用,它对其他人起作用,但对我不起作用。如果有人能帮忙的话,我很难确定这个问题 我试过用谷歌搜索类似的问题,但没有结果。从YouTube视频上观看,但他对此没有问题。这是我的密码 //This is importing the paddle from paddle.js import Paddle from "/src/paddle"; //This is JavaScript. It sets the canvas let canvas = document
//This is importing the paddle from paddle.js
import Paddle from "/src/paddle";
//This is JavaScript. It sets the canvas
let canvas = document.getElementById("gameScreen");
let ctx = canvas.getContext("2d");
//These variables will never change, and can be called upon
const GAME_HEIGHT = 800;
const GAME_WIDTH = 600;
/*This clears the screen, preventing the squares
from being redrawn over each other */
ctx.clearRect(0, 0, 800, 600);
//This is using the imported paddle and drawing it
let paddle = new Paddle(GAME_WIDTH, GAME_HEIGHT);
//On a seperate note, I am having trouble actually getting the paddle to draw
paddle.draw(ctx);
//This is the separate file I am importing from
export default class Paddle {
//Sets what the paddle dimensions will be
constructor(gameWidth, gameHeight) {
//sets the width of the paddle
this.width = 150;
//sets the height of the paddle
this.height = 30;
//sets the area where the paddle spawns
this.position = {
x: gameWidth / 2 - this.width / 2,
y: gameHeight - this.height - 10
};
}
//This actually ends up drawing the paddle on the board
draw(ctx) {
ctx.fillStyle = "#0f0";
ctx.fillRect(this.position.x, this.position.y, this.width, this.height);
}
}
我希望它能以绿色显示游戏屏幕底部中间的拨片,但没有显示任何内容,也没有错误消息。复制代码后,我注意到默认情况下画布大小只有300px x 150px。您试图绘制的挡板位于可见画布区域之外。使用:
ctx.canvas.width = GAME_WIDTH;
ctx.canvas.height = GAME_HEIGHT
在开始绘制画布之前设置画布的所需大小。
还要注意,清除画布时,使用的是硬编码值
ctx.clearRect(0,0,800,600)
-确保使用GAME\u WIDTH和GAME\u HEIGHT变量,以便在分辨率改变时,清除的区域保持一致
除此之外,您的
GAME\u WIDTH
为600,而GAME\u HEIGHT
为800-典型分辨率的宽度为800,高度为600。如果我的答案令人满意,请记住将其标记为已接受的答案。谢谢