简体   繁体   中英

Java opening images for sprites

I am making a game that needs multiple sets of sprites and they need to be stored in a form of 2d list/array. I have 2 objects that need sprites, you (which is the guy) and bee. I have the images sorted by these 2 characters, then their actions, then direction by the use of folders. 文件路径图

I made this code to open the "you" folder and insert the subfolder contents into a 2d arraylist that has moves down the side:

public void loadPic(){//open up all of the images and store them in an ArrayList
    File folder = new File("img/youImgs/run/right");
    File[] listOfFiles = folder.listFiles();
    ArrayList<BufferedImage> runImgs=new ArrayList<BufferedImage>();
    for (int i = 0; i < listOfFiles.length; i++) {
        File file = listOfFiles[i];
        if (file.isFile() && file.getName().endsWith(".png")) {
            try {
                runImgs.add(ImageIO.read(new File("img/youImg/run/right"+file.getName())));
            } 
            catch (IOException e) {
            }
        }
    }
    youImgs.add(runImgs);
}

I do not know how to modify it to make it useful and work.

I need them to save into separate lists so I can run them.

If you have a better idea on how to add sprites, please let me know.

Please help me out.

thank you in advance.

I found out how to do this. I edited my folder directory to have each move in a folder and then all of these folders in one youImg folder.

My code is below for anyone else that needs help.

public void loadPic(){//open up all of the images and store them in an ArrayList


    String youDirectory="img/youImgs/";
    File folder = new File(youDirectory);
    File[] listOfFiles = folder.listFiles();

    for(int i=0;i<listOfFiles.length;i++){
        youImgs.add(new ArrayList<BufferedImage>());
    }

    for(int h=0;h<listOfFiles.length;h++){
        File file=listOfFiles[h];
        if(file.isDirectory()){
            try{
                youMoves.add(file.getName());
                File folder2=new File(youDirectory+file.getName()+"/");
                File[]listOfFiles2=folder2.listFiles();
                for (int i = 0; i < listOfFiles2.length; i++){
                    File file2 = listOfFiles2[i];
                    if (file2.isFile() && file2.getName().endsWith(".png")){
                            youImgs.get(h).add(ImageIO.read(new File(youDirectory+file.getName()+"/"+file2.getName())));
                    }
                }
            }
            catch(IOException e){}
        }
    }
}

Thank you to many of the other questions on this site to help me find the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM