我正在尝试使用 p5.js 让球在画布上弹跳,但似乎width
, height
, and random()
没有定义。
这是我的全部代码:
function setup() {
createCanvas(640,480);
background(240);
}
var dot = {
x: random(width),
y: random(height),
size: 50,
r: 255,
g: 0,
b: 0,
speedX: 5,
speedY: 5
};
function draw() {
background(240);
fill(dot.r, dot.g, dot.b);
ellipse(dot.x, dot.y, dot.size, dot.size);
if (dot.x >= 615 || dot.x <= 25) { //if the ball is out of screen horizontally
dot.speedX = dot.speedX * -1;
}
if (dot.y >= 465 || dot.y <= 25) { //if the ball is out of screen vertically
dot.speedY = dot.speedY * -1;
}
dot.x = dot.x + dot.speedX;
dot.y = dot.y + dot.speedY;
}
如果我使用 JavaScriptMath.random
,效果很好。此外,如果我手动输入宽度和高度为数字(640 和 480),效果很好。 p5.js 不应该自动分配height
, width
, random
, ETC。?
这是怎么回事?