开发者

String Array iteration and startsWith()

I have a class thats responsible for animating some images. I have 3 player classes that each create their own instance of this animation class. Each player class sends a String path and a String array of file na开发者_运维知识库mes to my animation class. So what im doing is checking if the String array of file names starts with up, down, left or right. I then add them to an array of buffered images, 4 in total named up, down, left and right.

Now when the player wants to move left for example, the left[] will animate, same goes for the up direction etc. The problem is that only one image gets stored in each array. For example the up[] of buffered images holds only one image for up, while their should be 3 (there are 3 images for each direction). I cant figure it out.

The following code is taken from my Animation class that processes the arrays. Can someone tell me if I am missing something?

If this made absolutely no sense, my apologies :)...it sounded good in my head

thanks

try
    {
        for (String file : fileName)
        {
            String path = PATH + file + EXT;

            for (int i = 0; i < arrayLength; i++)
            {
                if (file.startsWith("u"))
                {
                    up[i] = ImageIO.read(new File(path));
                }

                if(file.startsWith("d"))
                {
                    down[i] = ImageIO.read(new File(path));
                }

                if (file.startsWith("l"))
                {
                    left[i] = ImageIO.read(new File(path));
                }

                if (file.startsWith("r"))
                {
                    right[i] = ImageIO.read(new File(path));
                }
            }
        }
    }
    catch (IOException e)
    {
        System.out.println("Could not load images: " + e);
    }
    catch (ArrayIndexOutOfBoundsException e)
    {
        System.out.println("Array out of bounds: " + e);
    }


I'm having some trouble following your description, but looking at your code here are my thoughts on what could potentially be your problem. First unrelated to your problem a coding suggestion, you have mutually exclusive cases (file.startsWith(...)) that you test with ifs, you should have else so that when one tests positive the others can be ignored:

for (String file : fileName) {
  String path = PATH + file + EXT;

  for (int i = 0; i < arrayLength; i++) {
    if (file.startsWith("u")) {
      up[i] = ImageIO.read(new File(path));
    } else if(file.startsWith("d")) {
      down[i] = ImageIO.read(new File(path));
    } else if (file.startsWith("l")) {
      left[i] = ImageIO.read(new File(path));
    } else if (file.startsWith("r")) {
      right[i] = ImageIO.read(new File(path));
    }
  }
}

As for the logic of the code, one potential problem I see is your array index does not seem to be properly linked to the file. As of now your code could be rewritten thusly without changing its effects:

for (String file : fileName) {
  String path = PATH + file + EXT;

  BufferedImage array = null;
  if (file.startsWith("u")) {
    array = up;
  } else if(file.startsWith("d")) {
    array = down;
  } else if (file.startsWith("l")) {
    array = left;
  } else if (file.startsWith("r")) {
    array = right;
  }

  for (int i = 0; i < arrayLength; i++) {
    array[i] = ImageIO.read(new File(path));
  }
}

You are simply writing a new buffered image with the same path to the entire array (or whatever portion is included by the variable "arrayLength") on each iteration of the outer loop. Because of this if "file" is the same type every time (i.e. startsWith is the same value), your array will only ever contain the last item in the fileName array.

Here is where I will start guessing at your intent. I assume that each String in fileName, is one path that should be then next image in the array of the animation for that particular direction, so what you need is a separate index into each array:

int upArrayIndex = 0, downArrayIndex = 0,
   leftArrayIndex = 0, rightArrayIndex = 0;
for (String file : fileName) {
  String path = PATH + file + EXT;

  if (file.startsWith("u")) {
    up[upArrayIndex++] = ImageIO.read(new File(path));
  } else if(file.startsWith("d")) {
    down[downArrayIndex++] = ImageIO.read(new File(path));
  } else if (file.startsWith("l")) {
    left[leftArrayIndex++] = ImageIO.read(new File(path));
  } else if (file.startsWith("r")) {
    right[rightArrayIndex++] = ImageIO.read(new File(path));
  }
}

I hope this is on the right track for the question you are asking, or at least gets you an idea where to start.


Here's a non-answer answer. Set a breakpoint in this method with a debugger. Are you running this code in an IDE like Eclipse? Root causing this problem will be much easier when you actually see the execution flow of your program - and its an essential tool for any developer who wants to truly understand some given code. As a side note, I think your problem has to do with arrayLength.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜