使用数组时图像 - 鼠标交叉/悬停效果问题

时间:2022-10-02 20:25:29

I'm working on a small exercise that involves making an interface of six die faces. The goal is to change the color of each die face when I hover over it. The issue I'm having is that I can only change the color of the first die face, not the proceeding. I've been reluctant to come here and ask because I feel my issue is so insignificant but I've been trying to get this to work for the last 4 days and I just can't figure it out. I feel there is something about iteration that I'm just quite not understanding yet.

我正在做一个小练习,涉及制作六个模具面的界面。我的目标是当我将鼠标悬停在它上面时改变每个模具面的颜色。我遇到的问题是我只能改变第一个模具面的颜色,而不是改变颜色。我一直不愿意来这里问,因为我觉得我的问题是如此微不足道,但我一直试图让这个问题在过去的4天内发挥作用,而我却无法理解。我觉得有一些关于迭代的东西我还没有理解。

Dice[] dice = new Dice[6];
void setup(){
  size(600,100);
  for(int i = 0; i < dice.length; i++){
    dice[i] = new Dice(i*100,0,100,100);
  }
  imageMode(CORNER);
}

void draw(){
  for(int i = 0; i < dice.length; i++){
    for(int j = 0; j < dice.length; j++){
      if(j!=i && dice[i].checkHover(mouseX,mouseY)){
        dice[i].drawDice(i,true);
      } else {
        dice[i].drawDice(i,false);
      }
    }
  }
}

class Dice{
  PImage[] diceFace = new PImage[6];
  PImage[] diceFaceHover = new PImage[6];
  int x;
  int y;
  int w;
  int h;

  Dice(int bx, int by, int bw, int bh){
    x = bx;
    y = by;
    w = bw;
    h = bh;
    for(int i = 0; i < dice.length; i++){//loads the images
      diceFace[i] = loadImage(i+".png");
      diceFaceHover[i] = loadImage(i+"h.png");
    }
  }

 void drawDice(int i, boolean hover){
   if(hover){
     image(diceFaceHover[i],x,y,w,h);
   } else {
     image(diceFace[i],x,y,w,h);
   }
 }

 boolean checkHover(float mx, float my){
   if((mx > x && mx < w) && (my > y && my < h)){
     return true;
   } else {
     return false;
   }
 }
}

I'll continue searching for a solution in the meantime.

在此期间我会继续寻找解决方案。

1 个解决方案

#1


2  

You have bad condition for checking hover. Don't forget that w and h are same for all dices but you need position not size.

你检查悬停的条件很差。不要忘记w和h对于所有骰子都是相同的,但你需要的位置不是大小。

if( (mx > x & mx < x+w) && ( my >y && my < y+h ) )

#1


2  

You have bad condition for checking hover. Don't forget that w and h are same for all dices but you need position not size.

你检查悬停的条件很差。不要忘记w和h对于所有骰子都是相同的,但你需要的位置不是大小。

if( (mx > x & mx < x+w) && ( my >y && my < y+h ) )