0

我正在为我正在制作的具有三种状态的交互式图形编写一些代码。我使用 switch 语句在展示一系列图像的状态之间移动。我上传了所有图像,但是当我调用一个包含 8 个图像数组的状态时,我上传了一条错误消息:ArrayIndexOutOfBoundsException 8

即使我的草图中加载了正确数量的图像,我似乎也无法摆脱它。

下面是我的代码,我正在使用 Processing 2.0b7 开发 Macbook Pro OSX

PImage[] teams;
int n = 24;
PImage[] teams2;
int m = 16;
PImage [] quarterFinalWinners;
int o = 7;
PImage bg;
float damping = 0.1;
PFont font;
String[] positions, prevPositions;
float x;
float y;

int afconState = 3;

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

public void setup() {
  size(600, 1600);
   bg = loadImage("football-pitch-hi.png");

  positions = new String[]{"18", "26", "32", "45", "58", "56", "59", "61", "66", "69", "71", "85", "98", "100", "116", "133"};
  prevPositions = new String[]{"14", "19", "25", "30", "34", "45", "52", "69", "71", "72", "87", "84", "89", "105", "107", "110"};
  int a = 16;

  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 + ".gif");
    }

   quarterFinalWinners = new PImage[o];
    for (int o = 0; o < quarterFinalWinners.length; o++); {
   quarterFinalWinners[o] = loadImage(o + "_.png");
   }
}

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

void draw() {
  background(#000000);
  if ((x < width) && (y < height)) {
for (int i = 0; i < 16; i++);
    }


  }

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

public void keyPressed() {
  noLoop();

  switch(afconState) {
  case 1:
   afconState = 1;
   background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams[i], 150, 60*i);
  }
  }

  if ((key == 's') || (key == 'S')) {

    afconState = 2;
    background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams2[i], 150, 60*i);
  }
  }

  else if ((key == 'q') || (key == 'Q')) {
    afconState = 3;
    background(#000000);
  for (int i = 0; i < 16; i++) {
  image(quarterFinalWinners[i], 150, 60*i);
  }
  }
  break;

/**----------------------------------------------------------------------------------------------------------------------------**/    
    case 2:
    if((key == 's') || (key == 'S')) {

    afconState = 2;
  background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams2[i], 150, 60*i);
  }
  }

  else if ((key == 'q') || (key == 'Q')) {
    afconState = 3;
  for (int i = 0; i < 16; i++) {
  image(quarterFinalWinners[i], 150, 60*i);
  }
  }

  break;

  case 3:
  background(#000000);
  if ((key == 'q') || (key == 'Q')) {
    afconState = 3;
  for (int i = 0; i < 16; i++) {
  image(quarterFinalWinners[i], 150, 60*i);
  }
  }

  else if((key == 's') || (key == 'S')) {

    afconState = 2;
  background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams2[i], 150, 60*i);
  }
  }

  break;

  default:
  afconState = 1;
   background(#000000);
  for (int i = 0; i < 16; i++) {
  image(teams[i], 150, 60*i);
  }  

  if ((key == 's') || (key == 'S')) {
    afconState = 2;
  // modify the positions list
  for (int i = 0; i < 16; i++) {
  image(teams2[i], 150, 60*i);
  }
  }

  else if ((key == 'q') || (key == 'Q')) {
    afconState = 3;
  for (int i = 0; i < 16; i++) {
  image(quarterFinalWinners[i], 150, 60*i);
  }
  }

  }
  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
  // redraw now that our state has changed

  redraw();
}
4

1 回答 1

0

大多数语言从 0 开始索引(即 8 项将被索引为 0 到 7) - 看起来您正在尝试引用索引为 8 的数组项,该数组项可能不存在。

于 2013-02-05T10:05:36.013 回答