如何在按键时更改PShape的颜色?

huangapple go评论77阅读模式
英文:

How to change the color of a PShape on key press?

问题

我正在尝试在按下键时更改PShape的颜色,以作为按下该键的标记。是否有一种方法可以在不使用beginShape()endShape()以及新的填充重新定义PShape的情况下实现这一点?

以下是我尝试的示例:

  1. PShape s;
  2. void setup() {
  3. size(100, 100);
  4. s = createShape(RECT, 25, 25, 50, 50);
  5. s.setFill(color(75, 183, 19));
  6. shape(s);
  7. }
  8. void draw() {
  9. if (keyPressed == true) {
  10. if (key == 'w') {
  11. s.setFill(color(217, 93, 47));
  12. }
  13. }
  14. }

setFill()函数在将形状放入窗口之前起作用,但在draw()函数中不起作用。我还尝试使用fill()

我还尝试在声明形状并在重新绘制形状之前更改填充时使用disableStyle()

  1. PShape s;
  2. void setup() {
  3. size(100, 100);
  4. shape();
  5. fill(75, 183, 19);
  6. shape(s);
  7. }
  8. void draw() {
  9. if (keyPressed == true) {
  10. if (key == 'w') {
  11. fill(217, 93, 47);
  12. shape();
  13. }
  14. }
  15. }
  16. void shape() {
  17. s = createShape(RECT, 25, 25, 50, 50);
  18. s.disableStyle();
  19. }

这也不起作用,形状仍然保持为第一种颜色。我是否遗漏了任何函数可以帮助我实现这一点?

英文:

I am trying to change the color of a PShape when I press a key, as a marker that that key is pressed. Is there a way that I can do this without redefining the PShape using beginShape() and endShape() with a new fill every time?

Here is an example of what I am attempting to do:

  1. PShape s;
  2. void setup() {
  3. size(100,100);
  4. s = createShape(RECT,25,25,50,50);
  5. s.setFill(color(75,183,19));
  6. shape(s);
  7. }
  8. void draw() {
  9. if(keyPressed == true) {
  10. if(key == 'w') {
  11. s.setFill(color(217,93,47));
  12. }
  13. }
  14. }

the setFill() function works before I actually put the shape into the window, but it won't in the draw() function. I have also tried using fill().

I have also tried using disableStyle() when declaring the shape and changing the fill before redrawing the shape:

  1. PShape s;
  2. void setup() {
  3. size(100,100);
  4. shape();
  5. fill(75,183,19);
  6. shape(s);
  7. }
  8. void draw() {
  9. if(keyPressed == true) {
  10. if(key == 'w') {
  11. fill(217,93,47);
  12. shape();
  13. }
  14. }
  15. }
  16. void shape() {
  17. s = createShape(RECT,25,25,50,50);
  18. s.disableStyle();
  19. }

This also doesn't work, the shape just stays as the first color. Is there any function I am missing which will help me to achieve this?

答案1

得分: 0

以下是代码的翻译部分:

  1. PShape s;
  2. color chosenColor;
  3. void setup() {
  4. size(100,100);
  5. chosenColor = color(random(255), random(255), random(255));
  6. }
  7. void draw() {
  8. myShape();
  9. }
  10. void myShape() {
  11. s = createShape(RECT,25,25,50,50);
  12. s.setFill(chosenColor);
  13. shape(s);
  14. }
  15. void keyPressed() {
  16. if (key == 'w') {
  17. chosenColor = color(random(255), random(255), random(255));
  18. }
  19. }
英文:

The following code will change the color of a PShape square each time a designated key is pressed:

  1. PShape s;
  2. color chosenColor;
  3. void setup() {
  4. size(100,100);
  5. chosenColor = color(random(255), random(255), random(255));
  6. }
  7. void draw() {
  8. myShape();
  9. }
  10. void myShape() {
  11. s = createShape(RECT,25,25,50,50);
  12. s.setFill(chosenColor);
  13. shape(s);
  14. }
  15. void keyPressed() {
  16. if (key == 'w') {
  17. chosenColor = color(random(255), random(255), random(255));
  18. }
  19. }

huangapple
  • 本文由 发表于 2023年3月31日 22:20:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899619.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定