我对p5.js真的很陌生,而且我试图找到解决方案几天没有运气。
我有一个按钮和一个椭圆。单击按钮时,画布上会出现一个矩形,当单击椭圆时,其颜色会发生变化。问题是我不能同时使用这两个颜色,原因是这部分代码:
function setup(){
createCanvas(800, 600);
bubble = new new_ellipse(500,300);
}
function draw() {
background(51);
button = createButton("clickme");
button.position(100, 65);
button.mousePressed(show_rect);
bubble.display();
stroke(rgb);
strokeWeight(2);
}
function mousePressed(){
bubble.clicked();
}
function new_ellipse(x,y){
this.x = x;
this.y = y;
this.col = color(255,100);
this.display = function(){
stroke(255);
fill(this.col);
ellipse(this.x, this.y, 100, 100);
}
this.clicked = function(){
var d = dist(mouseX, mouseY, this.x, this.y);
if(d < 50){
this.col = color(233,11,75);
}
}
}
function show_rect(){
fill(255,24,23);
rect(200,200,100,100);
}
如果我在我的代码上有它,当我点击按钮时会出现矩形,但是当我点击椭圆时没有任何反应。如果我将其从代码中删除,我可以更改椭圆的颜色,但是当我单击按钮时没有任何反应。 我不知道为什么这两行代码使得无法与按钮和形状进行交互,我真的很想知道。我错过了一些重要的事情吗?
我的代码如下:
scalacOptions += "-Xlog-implicits"