Strange overlapping of sprites with Sprite library, what's wrong?

edited December 2017 in Library Questions

Hello and good day.
I work with the Sprite library by Peter Lager.
My current project is a 2D platformer and I got a rly strange problem but I cant't find the exact source.
I restricted the problem of the overlapping Sprites to the WorldManager class in the void registerWorld(int index) function.
I use S4P.updateSprites(sw.getElapsedTime()); S4P.drawSprites();
to display the sprites.
It seems that all 3 worlds get registered by S4P at the same time.
The content of the 3 worldfiles I got here is like that,

OOOOOOOOOO

O________O

OZZZZZZZZO

//GameProject_Disconnect
import sprites.*;
import sprites.maths.*;
import sprites.utils.*;
import java.util.*;
import java.lang.*;

StopWatch sw;
WorldManager wm;

void setup() {
  size(500, 500);
  sw = new StopWatch();
  wm = new WorldManager(this);
}


void draw() {
  background(0);
  S4P.updateSprites(sw.getElapsedTime());
  S4P.drawSprites();
}

//WorldManager 

class WorldManager {
  FileManager fm;
  WorldTranslater wt;
  ArrayList<World> worlds;

  WorldManager(PApplet _pa) {
    fm = new FileManager();
    wt = new WorldTranslater(_pa);
    initWorlds();
    registerWorld(0);
  }


  void registerWorld(int index) {
    Sprite[][] tempW = worlds.get(index).getWorldMap();
    for (int i=0; i < 5; i++) {
      for (int j=0; j < 10; j++) {
        S4P.registerSprite(tempW[i][j]);
      }
    }
  }


  private void initWorlds() {
    worlds = new ArrayList<World>();
    for (int i=0; i < fm.getNumberOfWorlds(); i++) {
      World tempWorld = new World(
        fm.getFileContent(i), 
        wt.translateWorld(fm.getFileContent(i)), 
        fm.getTrimmedFilename(i), 
        fm.getWorldPath(i));
      worlds.add(new World());
      worlds.set(i, tempWorld);
    }
    println("Worlds successful loaded!");
  }
}

//FileManager

class FileManager {
  private String worldPath; // Path of the world files. 
  private String[] rawFilenames; // Filenames with prefix and suffix.
  private String[] trimmedFilenames; // Filenames without prefix and suffix.
  private ArrayList<String[]> fileContent; // Save the file content here. 
  private int numberOfFiles; // Number of worlds found.

  FileManager() {
    worldPath = new String(sketchPath() + "/Worlds/");
    readFileDirectory();
    numberOfFiles = rawFilenames.length;
    trimFilenames();
    fileContent  = new ArrayList<String[]>();
    initFileContent();
  }
  // Give me the content of one file chosen by index.
  String[] getFileContent(int index) {
    return fileContent.get(index);
  }

  int getNumberOfWorlds() {
    return numberOfFiles;
  }

  String getWorldPath(int index) {
    return new String(worldPath + rawFilenames[index]);
  }

  String getRawFilename(int index) {
    return rawFilenames[index];
  }

  String getTrimmedFilename(int index) {
    return trimmedFilenames[index];
  }

  private void initFileContent() {
    for (int i=0; i < numberOfFiles; i++) {
      fileContent.add(new String[0]);
      fileContent.set(i, parseFile(rawFilenames[i]));
    }
  }

  // Get all filenames from the world directory.
  private void readFileDirectory() {
    rawFilenames = getFilenames(worldPath);
  }
  // Cut the prefix and suffix off.
  private void trimFilenames() {
    trimmedFilenames = cutFilenames(getFilenames(worldPath));
  }

  // Reade the content of the file.
  private String[] parseFile(String filename) {
    BufferedReader reader = createReader(worldPath + filename);
    String line = null;
    String[] tempWorld = new String[5];
    int counter = 0;
    try {
      while ((line = reader.readLine()) != null) {
        tempWorld[counter] = line;
        counter++;
      }
      reader.close();
    } 
    catch (IOException e) {
      e.printStackTrace();
    }
    return tempWorld;
  }

  // Return all the raw filenames in a directory as an array of Strings.
  private String[] getFilenames(String dir) { 
    File file = new File(dir);
    if (file.isDirectory()) { 
      String[] names = file.list();
      return names;
    } else {
      // If it's not a directory.
      println("ERROR: There is no worlds directory!");
      return null;
    }
  }

  // Delete the prefix and the suffix of the filename.
  private String[] cutFilenames(String[] filenames) {
    String[] names = filenames;
    for (int i=0; i < names.length; i++) { 
      names[i] = names[i].substring(2, names[i].length()-4); // -4 for the ".txt" suffix.
    }
    return names;
  }
}

//WorldTranslater

class WorldTranslater {
  PApplet pa;
  // Agreements for the height and width of a world.
  final int mapWidth = 10;
  final int mapHeight = 5;

  private String[] crypticWorld;
  private Sprite[][] translatedWorld;

  final String wall = "Sprites/Wall.png";
  final String background = "Sprites/Background.png";
  final String ground = "Sprites/Ground.png";
  final String platform = "Sprites/Platform2.png";
  final String nothing = "Sprites/Nothing.png";

  final int viewDistance = 1; // The higher the number, the closer is the picture in the foreground.

  WorldTranslater(PApplet _pa) {
    pa = _pa;
    crypticWorld    = null;
    translatedWorld = new Sprite[mapHeight][mapWidth];
  }
  // Give me the fancy sprite array from this cryptical String array plz.
  public Sprite[][] translateWorld(String[] cw) {
    crypticWorld = cw;
    translateCWorld();
    return translatedWorld;
  }

  // Translate the crypticWorld to a world with fancy sprites.
  private void translateCWorld() {
    Sprite sp; 
    for (int i=0; i < mapHeight; i++) {
      for (int j=0; j < mapWidth; j++) {
        sp = distinguishSprite(crypticWorld[i].charAt(j));
        sp.setPos(new Vector2D(sp.getWidth()*j, sp.getHeight()*i));
        translatedWorld[i][j] = sp;
      }
    }
  }

  // Check which token is which sprite.
  private Sprite distinguishSprite(char c) {
    if (c == 'O') {
      return new Sprite(pa, wall, viewDistance);
    } else if (c == 'X') {
      return new Sprite(pa, background, viewDistance);
    } else if (c == 'Z') {
      return new Sprite(pa, ground, viewDistance);
    } else if (c == '_') {
      return new Sprite(pa, platform, viewDistance);
    } else if (c == ' ') {
      return new Sprite(pa, nothing, viewDistance);
    }
    println("ERROR: Invalid token");
    return null;
  }
};

//WorldObjekt

class World {
  private String[] rawWorldMap;
  private Sprite[][] worldMap;
  private String name;
  private String path;

  World() {
    rawWorldMap = null;
    worldMap = null;
    name = null;
    path = null;
  }

  World(String[] _rawWorldMap, Sprite[][] _worldMap, String _name, String _path) {
    rawWorldMap = _rawWorldMap;
    worldMap = _worldMap;
    name = _name;
    path = _path;
  }

  public Sprite[][] getWorldMap() {
    return worldMap;
  }

  String[] getRawWorldMap() {
    return rawWorldMap;
  }

  String getName() {
    return name;
  }

  String getPath() {
    return path;
  }
};

Answers

  • edited December 2017

    This problem appeared as I used ArrayList to store the sprites.

  • This problem appeared as I used ArrayList to store the sprites.

    I can't find an ArrayList of Sprite objects in the code above.

    I assume that at some time the program worked, so what were the actual changes you made.

    The type of problem you have is called a logic error, in other words your program compiles and runs but does not do what you expect it to. This type of error is almost impossible to find without being able to run the program.

    If you want help you need to make the sketch available for others to run, and in this case a picture showing what you expect the program to show.

    You can make the sketch available by selecting the Archive Sketch option from the menus. This will create a zip file with all the code and resources etc. You would then need to find somewhere on the web where we can download this file.

  • The download tab didn't work for me. :(

  • edited December 2017

    Best place to make a project available is at GitHub: https://GitHub.com B-)

    You can even drag & drop your entire sketch folder into your repo's page there: \m/
    https://Help.GitHub.com/articles/adding-a-file-to-a-repository/

  • edited January 2018

    .

  • Answer ✓

    Okay I bypass that problem if I add to every Sprite, I create in the cWorldTranslater class, .setVisible(false);

Sign In or Register to comment.