简体   繁体   English

在p5.js中创建钢琴

[英]Creating a piano in p5.js

I am creating a piano using p5.js. 我正在使用p5.js制作钢琴。 I need help with the color change. 我需要换色方面的帮助。 When a user presses a key, I want the key to flash a quick color change to let them know that they pressed the key. 当用户按下某个键时,我希望该键闪烁一次快速的颜色更改,以使他们知道他们按下了该键。

In my code, the color does change when you click on the first key, however, when I click a little bit outside the first key, the first key still changes color. 在我的代码中,单击第一个键时颜色确实会更改,但是,当我在第一个键之外单击一点时,第一个键仍会更改颜色。

Is my distance a little off? 我的距离有点远吗? Or is there a more effective way to do this? 还是有更有效的方法来做到这一点?

function setup() {
  createCanvas(990, 600);
}

function draw() {
  background(220);
  fill(255);
  rect(0, 300, 70, 400);
  rect(70, 300, 70, 400);
  rect(140, 300, 70, 400);
  rect(210, 300, 70, 400);
  rect(280, 300, 70, 400);
  rect(350, 300, 70, 400);
  rect(420, 300, 70, 400);
  rect(490, 300, 70, 400);
  rect(560, 300, 70, 400);
  rect(630, 300, 70, 400);
  rect(700, 300, 70, 400);
  rect(770, 300, 70, 400);
  rect(840, 300, 70, 400);
  rect(910, 300, 70, 400);
  fill(0);
  rect(50, 300, 38, 180);
  rect(120, 300, 38, 180);
  rect(260, 300, 38, 180);
  rect(330, 300, 38, 180);
  rect(400, 300, 38, 180);
  rect(540, 300, 38, 180);
  rect(610, 300, 38, 180);
  rect(750, 300, 38, 180);
  rect(820, 300, 38, 180);
  rect(890, 300, 38, 180);
  text("mouse x: "+mouseX+" mouse y:"+mouseY, width/2,height-30);
 }

function mousePressed() {
  cursor(HAND);

}

function mouseReleased() {
    cursor(ARROW);

let d = dist(mouseX, mouseY, 0, 300);
  if (d < 300) {
    fill(0);
    rect(0, 300, 70, 400);
  }
}

You're checking whether the mouse position is withing 300 pixels of the point 0,300. 您正在检查鼠标位置是否在0,300点的300个像素处。 This sets up a circle with a center of 0,300 and a radius of 300. Try drawing that in your scene to see where your clickable area is. 这将设置为0,300中心和300尝试绘制在场景中看到你的点击区域为半径的

Your keys are rectangles, so you should be using point- rectangle intersection to check whether the mouse is inside a particular key. 您的按键是矩形,因此您应该使用点- 矩形相交来检查鼠标是否在特定按键内。 Google is your friend here, but basically you want to check whether mouseX is between the left and right edges, and mouseY is between the top and bottom edges. Google是您的朋友,但基本上您想检查mouseX是否在左右边缘之间,而mouseY是否在上下边缘之间。

Finally, note that you're only ever showing the "flash" for a single frame. 最后,请注意,您只显示单个帧的“闪光”。 I personally can't even see it. 我个人甚至看不到它。 You probably want to show the flash for longer, using the millis() function or the frameCount variable for timing. 您可能希望使用millis()函数或frameCount变量进行计时,以显示闪光灯更长的时间。 (Again, Google is your friend!) (再次,Google是您的朋友!)

Shameless self-promotion: here is a tutorial on collision detection, including point-rectangle intersection. 无耻的自我促进: 是有关碰撞检测的教程,包括点与矩形的交点。 It's for Processing, but the ideas are the same in P5.js. 它用于处理,但思想在P5.js中相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM