mousePressed事件在keyPressed处理之前执行

时间:2013-02-01 15:43:02

标签: loops processing keypress mousepress

这个错误严重破坏了我的一周。我正在尝试创建一个交互式排行榜,其中有三个数组:1个带有图像,2个带有我写成字符串的整数。我正在尝试制作一个keyPressed事件,它会使代表团队的图像随着它们上升或下降而变化,我有一个mousePressed事件来执行循环以将窗口恢复到其原始状态。

我的问题是当我尝试运行代码时,keyPressed事件没有执行,只有在我点击鼠标后才这样做。然后图像移动但字符串数组不会循环回第一组图像。我已经包含了下面的代码......我知道它很冗长,并且相信我一直在折射和缩短。现在我想要帮助的是确保keyPressed事件首先执行,并且当循环执行时,positions1字符串数组恢复到其原始位置。

我已将下面的代码包含在内,并正在开发Macbook Pro OSX Processing 2.0b7。

我重新编写了我的代码并使用循环来放置图像和文本。现在我遇到的问题是,当我启动keyPressed事件时,图像和文本不会改变。你能看一下我的代码:

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();
      }

1 个答案:

答案 0 :(得分:1)

首先减少您的代码。为了证明问题以便其他人可以提供帮助,可以删除大部分代码。例如,两个图像和两个整数应该足够,只需要最小的keyPressed和mousePressed函数。

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

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

您的代码也不会交换数组,它会重新分配数组变量以指向同一个东西。在keyPressed上,“position1 = position2”意味着position2当然是position2,但position1也是position2。您现在有两个指向同一个数组的变量。您通过position1或position2所做的任何更改现在都会更改相同的数组,而任何进一步按下或执行“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();
}