私は、p5.jsを使ってキャンバスの周りにボールを跳ね返そうとしていますが、width
,height
、およびrandom()
は定義されていないようです。 はここにすべての私のコードです:p5.js - random()、height、およびwidthは定義されていませんか?
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;
}
私にはJavaScriptのMath.random
を使用している場合は、それが正常に動作します。さらに、手動で幅と高さを数値(640と480)として入力するとうまく動作します。 p5.jsは自動的にheight
,width
,random
などを割り当てるべきではありませんか? 何が起こっているのですか?