Javascript 在p5编辑器上运行此代码时,第0行出现错误
//下面的代码块就是我遇到问题的代码块。气泡不会从屏幕的垂直边缘反弹Javascript 在p5编辑器上运行此代码时,第0行出现错误,javascript,processing,p5.js,Javascript,Processing,P5.js,//下面的代码块就是我遇到问题的代码块。气泡不会从屏幕的垂直边缘反弹 class Bubble { constructor() { var allInstances = []; createCanvas(600,600); var radius = random(10,100); this.x = random(0, 600); this.y = random(0, 600); this.width = ra
class Bubble {
constructor() {
var allInstances = [];
createCanvas(600,600);
var radius = random(10,100);
this.x = random(0, 600);
this.y = random(0, 600);
this.width = radius;
this.height = radius;
var x = random(0,300);
var y = random(0,300);
var z = random(0,300);
this.velocityX = random(-5, +5);
this.velocityY= random(-5, +5);
this.display = function() {
stroke(10);
fill(x,y,z);
ellipse(this.x, this.y, this.width, this.height);
}
编辑: 程序运行时,多个气泡以上面显示的方式出现,但我无法让它们从画布的侧面反弹。你能帮我吗?回答: […]但我无法让它们从画布的侧面反弹。[……] 您必须评估气泡是否撞击画布的一侧,并在
气泡中更改气泡的方向。移动:
类气泡{
// [...]
构造函数(){
// [...]
this.move=函数(){
this.x=this.x+this.velocityX;
this.y=this.y+this.velocityY;
如果(this.x>600-this.radius | | this.x600-this.radius | | this.y
哦,谢谢!那么我将把createCanvas(600600)
放在哪里呢?rabbi76,程序运行时,会以上面显示的方式出现多个气泡,但我无法让它们从画布的侧面反弹。你能帮我吗?看看答案。
if(this.x > 600 || this.x < 0) {
this.velocityX = this.velocityX * -1;
}
this.move = function() {
this.x = this.x + this.velocityX;
this.y = this.y + this.velocityY;
}
}
}