[英]Java opening images for sprites
我正在制作一個需要多組精靈的游戲,它們需要以2d列表/數組的形式存儲。 我有2個需要精靈的對象,您(這是男人)和蜜蜂。 我按這2個字符對圖像進行排序,然后對它們進行操作,然后按使用文件夾的方向進行排序。
我編寫了以下代碼以打開“ you”文件夾,並將子文件夾的內容插入到向下移動的2d arraylist中:
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);
}
我不知道如何修改它以使其有用並起作用。
我需要將它們保存到單獨的列表中,以便我可以運行它們。
如果您對添加子畫面有更好的想法,請告訴我。
請幫幫我。
先感謝您。
我發現了如何做到這一點。 我編輯了文件夾目錄,以使每個移動到一個文件夾中,然后將所有這些文件夾都放在一個youImg文件夾中。
以下是我需要其他幫助的代碼。
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){}
}
}
}
感謝您在此站點上的許多其他問題,以幫助我找到答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.