Loops mousePressed事件在按键处理之前执行

Loops mousePressed事件在按键处理之前执行,loops,processing,keypress,mousepress,Loops,Processing,Keypress,Mousepress,这只虫子严重地毁了我的一周。我正在尝试创建一个交互式排行榜,其中有三个数组:1个包含图像,2个包含我编写的字符串形式的整数。我正在尝试制作一个按键事件,它将使数字随着代表团队上下阶梯的图像而改变,我有一个鼠标按下事件来执行循环,将窗口恢复到其原始状态 我的问题是,当我试图运行代码时,按键事件不会执行,只有在我单击鼠标后才会执行。然后图像会移动,但字符串数组不会与第一组图像循环。我已经包括了下面的代码…我知道它很长,相信我一直在折射和缩短我去。现在我想要帮助的是确保先执行keyPressed事件,

这只虫子严重地毁了我的一周。我正在尝试创建一个交互式排行榜,其中有三个数组:1个包含图像,2个包含我编写的字符串形式的整数。我正在尝试制作一个按键事件,它将使数字随着代表团队上下阶梯的图像而改变,我有一个鼠标按下事件来执行循环,将窗口恢复到其原始状态

我的问题是,当我试图运行代码时,按键事件不会执行,只有在我单击鼠标后才会执行。然后图像会移动,但字符串数组不会与第一组图像循环。我已经包括了下面的代码…我知道它很长,相信我一直在折射和缩短我去。现在我想要帮助的是确保先执行keyPressed事件,然后执行循环时positions1字符串数组恢复到其原始位置

我已经在下面加入了我的代码,正在开发MacBookPro OSX Processing 2.0b7

我折射了我的代码,并使用循环来放置图像和文本。现在的问题是,当我启动按键事件时,图像和文本不会改变。请看一下我的代码:

PImage[] teams;
int n = 24;
PImage[] teams2;
int m = 16;
PImage quarterFinalWinners = new PImage();
float damping = 0.1;
PFont font;
String[] positions1 = {"18", "26", "32", "45", "58", "56", "59", "61", "66", "69", "71", "85", "98", "100", "116", "133"};
String[] positions2 = {"14", "19", "25", "30", "34", "45", "52", "69", "71", "72", "87", "84", "89", "105", "107", "110"};
float x;
float y;

/**----------------------------------------------------------------------------------------------------------------------------**/

void setup() {
  size(600, 1600);
  frameRate(60);
  smooth();

  font = loadFont("Calibri-Bold-48.vlw");
  textFont(font);

  frame.setResizable(true);

  teams = new PImage[n];
    for (int i = 0; i < teams.length; i++) {
      teams[i] = loadImage(i + ".png");
    }

  teams2 = new PImage[m];
    for (int i = 0; i < teams2.length; i++) {
      teams2[i] = loadImage(i + ".jpg");
    }
}

/**----------------------------------------------------------------------------------------------------------------------------**/

void draw() {
  //noLoop();
  background(0);

  if ((x < width) && (y < height)) {
for (int i = 0; i < 16; i++) {
  image(teams[i], 150, 60*i);
  text(positions1[i], 100, 72*i);
     }
    }

 if (keyPressed) {
if((key == 's') || (key == 'S') && (x < width) && (y < height)) {
  for (int i = 0; i < 16; i++) {
  image(teams[i], 150, 60*i);
  text(positions1[i], 100, 72*i);
  }

   for (int i = 0; i >= 16; i++) {
  text(positions2[i], 100, 72*i); 
    }
   }
  }
}

/**----------------------------------------------------------------------------------------------------------------------------**/

/**void keyPressed () {
if((key == 's') || (key == 'S') && (x < width) && (y < height)) {
   for (int i = 0; i > 16; i++) {
  image(teams[i], 150, 60*i);

}

   for (int i = 0; i >= 16; i++) {
  text(positions2[i], 100, 72*i); 
    }
}
}**/

     /**image(images[10], 150, 290);
  image(images[19], 150, 50);
  image(images[17], 150, 230);
  image(images[2], 150, 110);
  image(images[22], 150, 410);
  image(images[20], 150, 470);
  image(images[16], 150, 650);
  image(images[6], 150, 350);
  image(images[7], 150, 590);
  image(images[18], 150, 770);
  image(images[21], 150, 170);
  image(images[12], 150, 830);
  image(images[13], 150, 530);
  image(images[23], 150, 950);**/

void mousePressed () {
      if (mousePressed) {
      positions2 = positions1;
      }
     loop();
      }
PImage[]团队;
int n=24;
PImage[]团队2;
int m=16;
PImage quarterFinalWinners=新PImage();
浮动阻尼=0.1;
字体;
字符串[]位置1={“18”、“26”、“32”、“45”、“58”、“56”、“59”、“61”、“66”、“69”、“71”、“85”、“98”、“100”、“116”、“133”};
字符串[]位置2={“14”、“19”、“25”、“30”、“34”、“45”、“52”、“69”、“71”、“72”、“87”、“84”、“89”、“105”、“107”、“110”};
浮动x;
浮动y;
/**----------------------------------------------------------------------------------------------------------------------------**/
无效设置(){
尺寸(6001600);
帧率(60);
光滑的();
font=loadFont(“Calibri-Bold-48.vlw”);
文本字体(字体);
frame.setresizeable(true);
团队=新PImage[n];
for(int i=0;i=16;i++){
文本(位置2[i],100,72*i);
}
}
}
}
/**----------------------------------------------------------------------------------------------------------------------------**/
/**按下void键(){
如果((键='s')| |(键='s')&&&(x<宽度)和&(y<高度)){
对于(int i=0;i>16;i++){
图像(团队[i],150,60*i);
}
对于(int i=0;i>=16;i++){
文本(位置2[i],100,72*i);
}
}
}**/
/**图像(图像[10],150290);
图像(图像[19],150,50);
图像(图像[17],150230);
图像(图像[2],150,110);
图像(图像[22],150410);
图像(图像[20],150470);
图像(图像[16],150650);
图像(图像[6],150350);
图像(图像[7],150590);
图像(图像[18],150770);
图像(图像[21],150,170);
图像(图像[12],150830);
图像(图像[13],150530);
图像(图像[23],150950)**/
鼠标按下(){
如果(鼠标按下){
位置S2=位置S1;
}
loop();
}

首先减少代码。为了演示问题以便其他人能够提供帮助,可以删除大部分代码。例如,两个图像和两个整数就足够了,只需要最少的按键和鼠标按键功能

也就是说,代码中也存在实际问题。例如,在mousePressed中,不测试是否(mousePressed)。是的,这就是调用mousePressed()的原因。您还可以调用loop(),它什么也不做。loop()和noLoop()函数仅确定draw()完成后是否再次调用draw()。loop()将草图模式从“基于事件”更改为“恒定帧速率”,noLoop()则相反-您真正想做的是在必要时触发重画,在事件处理结束时使用redraw()

默认的处理帧率是60,所以帧率(60)不会做任何事情。您还使用了.vlw字体,它实际上不是一种字体,而是一种图像格式。font=createFont(“Calibri.ttf”,16);更安全,因为它将在拒绝.vlw字体的系统上运行(例如在浏览器中运行草图时)

您的代码也不会交换数组,它会重新分配数组变量以指向相同的对象。按键时,“位置S1=位置S2”表示位置S2当然是位置S2,但位置S1也是位置S2。现在有两个变量指向同一数组。您通过positions1或positions2所做的任何更改现在都将更改相同的数组,任何进一步的键或鼠标按“positions2=positions1”都将不起任何作用,因为它们已经指向相同的对象,因此重新分配将保留这一点

因此,为了更好地帮助您,这里有一些代码可以大致满足您的需求,让您开始学习,但我强烈建议您在编程时可以在某个地方提问(如处理IRC频道或论坛),因为现在代码中有很多使用错误的函数和想法,这意味着你还不懂语言,你想让其他人看片段来判断你做的是明智的还是非常错误的

String[] positions, prevPositions;

void setup() {
  // setup two identical-content arrays
  positions = new String[]{"1","a","b","c"};
  prevPositions = new String[]{"1","a","b","c"};
  // don't draw at a constant framerate. we'll redraw
  // based on key/mouse events
  noLoop();
}

void draw() {
  // white background, black text
  background(255);
  fill(0);
  // just draw the first thing in "position"
  text(positions[0], width/2, height/2);
}

void keyPressed () {
  // cache what the array looks like
  arrayCopy(positions,0,prevPositions,0,positions.length);
  // modify the positions list
  positions[0] = ""+ (int(positions[0])+1);
  // redraw now that our state has changed
  redraw();
}

void mousePressed () {
  // revert to previous array. We can only do this once.
  // if someone pressed the key seven times, we can't revert
  // seven times, because the code only has one history state
  // that is updated every time a key is pressed
  arrayCopy(prevPositions,0,positions,0,positions.length);
  // redraw now that our state has changed
  redraw();
}

您的代码有很多地方可以改进,比如