[英]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.