简体   繁体   English

从我的关卡编辑器中保存切片精灵的好方法是什么?

[英]Whats a good way to save tile sprites from my level editor?

I have built a tile based level editor in java, It is just about completed, but as I was writing the method to save the level, that all of my tiles were image sprites and I dont know of a way to save them so that the actual game can read them without making each tile it's own image file which I would then need to import to read in the real game. 我已经在Java中建立了一个基于图块的关卡编辑器,该编辑器将要完成,但是当我编写保存该关卡的方法时,我所有的图块都是图像精灵,因此我不知道保存它们的方法,因此实际的游戏可以读取它们,而无需将每个图块都变成自己的图像文件,然后我需要将其导入才能在真实游戏中读取。 (there are over 700 possible tiles, a level would probably use 100 of those); (可能有700多个磁贴,一个关卡可能会使用100个磁贴);

My question to you all: what is the best way to save all these tiles? 我对大家的问题是:保存所有这些图块的最佳方法是什么? I am familiar with saving things to a text file but I am not able to save these images in the same way. 我熟悉将内容保存到文本文件中,但是无法以相同的方式保存这些图像。 Is there a way to save the level so that my text based information is saved in the same place as my tile sprites? 有没有一种方法可以保存关卡,以使基于文本的信息与平铺图片保存在同一位置?

here is my tile class and what I have so far in terms of my save method 这是我的tile类,以及到目前为止我保存方法的内容

    public class Tile {
     int x, y, w, h, type;
     BufferedImage layer1Image;
     BufferedImage layer2Image;
     BufferedImage layer3Image;
     BufferedImage layer4Image;
     boolean walkable;

     public Tile(int x, int y, int w, int h) {
      walkable = false;
      this.x = x;
      this.y = y;
      this.w = w;
      this.h = h;
      type = 0;
      layer1Image = null;
      layer2Image = null;
      layer3Image = null;
      layer4Image = null;
     }

     public Tile() {

     }

     public int getX() {
      return x;
     }

     public int getY() {
      return y;
     }

     public int getW() {
      return w;
     }

     public int getH() {
      return h;
     }

     public void setX(int newX) {
      x = newX;
     }

     public void setY(int newY) {
      y = newY;
     }

     public void setW(int newW) {
      w = newW;
     }

     public void setH(int newH) {
      h = newH;
     }

     public boolean mouseOver(int mouseX, int mouseY) {
      if (mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h) {
       return true;
      }
      return false;
     }

     public int getType() {
      return type;
     }

     public boolean getWalkable() {
      return walkable;
     }

     public BufferedImage getLayer1Image() {
      return layer1Image;
     }

     public BufferedImage getLayer2Image() {
      return layer2Image;
     }

     public BufferedImage getLayer3Image() {
      return layer3Image;
     }

     public BufferedImage getLayer4Image() {
      return layer4Image;
     }

     public void clearImages() {
      layer1Image = null;
      layer2Image = null;
      layer3Image = null;
      layer4Image = null;
      walkable = true;
     }

     public void setImage(BufferedImage image, int layer) {
      switch (layer) {
      case 0:
       layer1Image = image;
       break;
      case 1:
       layer2Image = image;
       break;
      case 2:
       layer3Image = image;
      case 3:
       layer4Image = image;
      }
     }

     public void display(Graphics g, int x, int y, int w, int h, int zoom) {
      g.setColor(Color.BLACK);
      g.drawImage(layer1Image, x, y, w, h, null);
      g.drawImage(layer2Image, x, y, w, h, null);
      g.drawImage(layer3Image, x, y, w, h, null);
      g.drawImage(layer4Image, x, y, w, h, null);
      g.drawRect(x, y, w, h);

     }

     public void getAttributes(Tile tile) {
      layer1Image = tile.getLayer1Image();
      layer2Image = tile.getLayer2Image();
      layer3Image = tile.getLayer3Image();
      layer4Image = tile.getLayer4Image();
      x = tile.getX();
      y = tile.getY();
      w = tile.getW();
      h = tile.getH();
      walkable = tile.getWalkable();
      type = tile.getType();

     }

     public void setWalkable(boolean b) {
      walkable = b;
     }



          //  Here is the save, its in a different class in case my formatting in the post is screwy






        public void doSaveAsText() {
      if (fileDialog == null) {
       fileDialog = new JFileChooser();
      }
      File selectedFile;
      if (editFile == null) {
       selectedFile = new File("levelData.txt");
      } else {
       selectedFile = new File(editFile.getName());
      }
      fileDialog.setSelectedFile(selectedFile);
      fileDialog.setDialogTitle("Select File to be Saved");
      int option = fileDialog.showSaveDialog(this);
      if (option != JFileChooser.APPROVE_OPTION) {
       return;
      }
      selectedFile = fileDialog.getSelectedFile();
      if (selectedFile.exists()) {
       int response = JOptionPane.showConfirmDialog(this, "Already exists, overwrite?", "Confirm Save", JOptionPane.YES_NO_OPTION,
         JOptionPane.WARNING_MESSAGE);
       if (response != JOptionPane.YES_OPTION) {
        return;
       }

      }
      PrintWriter out;
      try{
       FileWriter stream = new FileWriter(selectedFile);
       out = new PrintWriter(stream);
      } catch (Exception e){
       JOptionPane.showMessageDialog(this, "error");
       return;
      }
      try{
       out.println("LevelEditor v1.0");
       for(int i = 0; i < mapPanel.tilesList.size(); i++){
        Tile currentTile = mapPanel.tilesList.get(i);
        out.println("startTile");
        out.println("" + currentTile.getX());
        out.println("" + currentTile.getY());
        out.println("" + currentTile.getW());
        out.println("" + currentTile.getH());
        out.println("" + currentTile.getWalkable());

        out.println("endTile");
       }
       out.close();
      }catch(Exception e){
       System.exit(0);
      }

     }



Any suggestions would be great

You could just save the whole higher in hierarchy Java objects to an output stream, and load them at a later stage. 您可以将整个更高层次的Java对象保存到输出流,并在以后加载它们。

See this example . 请参阅此示例

检出序列化API,或仅提取字节并从中生成十六进制字符串。

How about encapulating the BufferedImage into a TileImage class that holds a reference to a particular image, then you can output the image reference in your save file. 如何将BufferedImage TileImage到包含对特定图像的引用的TileImage类中,然后可以在保存文件中输出图像引用。 That way you don't have to save the image data over and over again, and updated graphics will be available to saved games. 这样,您不必一遍又一遍地保存图像数据,并且更新的图形将可用于保存的游戏。

I'm not sure if you're more concerned about the fact that your tile images are separate from your text information, or the fact that each tile image will be in a separate file from the other tiles. 我不确定您是否更担心以下事实:您的图块图像与文本信息是分开的,还是每个图块图像与其他图块位于单独的文件中。

For the first problem, you may be able to store your textual date in image metadata (eg: a comment). 对于第一个问题,您也许可以将文本日期存储在图像元数据中(例如:评论)。 Then your tiles would just be images files. 然后,您的图块将只是图像文件。

If you're more concerned about the fact that each tile image will be in a separate file from the other tiles then you could create a single image that consists of all of your tile images in a grid (or something similar). 如果您更担心每个图块图像将与其他图块位于单独的文件中的事实,则可以创建一个包含网格中所有图块图像(或类似图像)的单个图像。 You then have a single image file that you can also edit in standard image editing software if you desire. 然后,您将拥有一个图像文件,如果需要,也可以在标准图像编辑软件中进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中循环异步任务 - 最好的方法是什么? - Looping Async Tasks in Android - whats the good way? 从代码编辑器创建方法级别断点的任何方法? - Any way to create a method level breakpoint from code editor? 用Java检查和比较数据库中的数据(时间限制)的好方法是什么? - Whats the good way check and compare data (time limit) from database in Java? 从编程层模型的角度理解在Hibernate中使用Ask.save()和.commit()的正确方法是什么 - Understanding ask, Whats the correct way using .save() and .commit() in Hibernate from the programming layers model Perspective 什么是代表我的Yaml文件的更好方法 - whats is the better way to represent my yaml file 多选项卡地图/平铺级别编辑器中的CPU /内存效率(许多HashMaps / ArrayLists) - CPU/Memory Efficiency in Multi-tabbed Map/Tile Level Editor (many HashMaps/ArrayLists) 从地图图块构建级别packfile以在libGDX中使用 - Build level packfile from map tile for use in libGDX 什么是一个好的Java调试器? - Whats a good java debugger? 什么是在Firebase Android中使用同一对象列表保存对象的最佳方法 - Whats the best way to save an object with a list of the same object in firebase android Android-读取输入流并将其另存为文件的最有效方法是什么? - Android - Whats the most efficient way to read an inputstream and save as file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM