I don't understand why this doesn't work

I’m trying to make some functions that allow me to show some images in the window, so I made a class in which I put the PImage variables and a function that allows me to load the images, but it doesn’t actually work. (And yes, if you’re asking so, I’m trying to reproduce a kind of Pokémon game copy in Processing, and I’m completely aware this will be almost impossible). Here’s the source code of the two files:

gameLogic:

int pX=0, pY=0;

void setup() {

  tiles.imgLoader(tiles.pokeCenter, "pokeCenter");
  size(1080, 720);
  background(0);
}

void keyPressed() {

  switch(key) {
  case 'w':
    pY-=10;
    break;
  case 'a':
    pX-=10;
    break;
  case 's':
    pY+=10;
    break;
  case 'd':
    pX+=10;
    break;
  }
}

void draw() {

  background(0);
  for (int a=0; a<width; a+=10)
    for (int b=0; b<height; b+=10) image(grass, a, b, 10, 10);
  image(pokeCenter, pX, pY, 120, 120);
}

tilesLoader:

public class tiles {

  PImage pokeCenter;
  void imgLoader(PImage img, String fileName) {
    img=loadImage(fileName+".png");
  }
}

PS I know that it doesn’t do anything with a bit of sense, but it’s just a test for now, only to see if I can actually do what I wanted to do

1 Like

A couple of points before we look at what went wrong. By convention in Java all class names start with and uppercase letter so I suggest you change the class name from tiles to Tiles this makes it easier when reading the code to identify the classes. Second the call to size(...) method should be the first one in setup and the call to backgorund is superfluous because you use it in the draw() method. So change you code to

void setup() {
  size(1080, 720);
  tiles.imgLoader(tiles.pokeCenter, "pokeCenter");
}

No lets look at the problem here is your class with the name corrected

public class Tiles {

  PImage pokeCenter;

  void imgLoader(PImage img, String fileName) {
    img=loadImage(fileName+".png");
  }
}

It has just one method which you call with

  tiles.imgLoader(tiles.pokeCenter, "pokeCenter");

which will not work because tiles is the name pf a class not an actual object of that class so correcting all these gives us

int pX=0, pY=0;

Tiles tiles = new Tiles();

void setup() {
  size(1080, 720);
  tiles.imgLoader(tiles.pokeCenter, "pokeCenter");
}

void keyPressed() {
  switch(key) {
  case 'w':
    pY-=10;
    break;
  case 'a':
    pX-=10;
    break;
  case 's':
    pY+=10;
    break;
  case 'd':
    pX+=10;
    break;
  }
}

void draw() {

  background(0);
  for (int a=0; a<width; a+=10)
    for (int b=0; b<height; b+=10) image(grass, a, b, 10, 10);
  image(pokeCenter, pX, pY, 120, 120);
}

public class Tiles {

  PImage pokeCenter;
  
  void imgLoader(PImage img, String fileName) {
    img=loadImage(fileName+".png");
  }
}

But you still have problems because you are trying to say where to store the image i.e. in pokeCenter by specifying the variable name using a string which won’t work.

Consider the statement PImage pokeCenter; inside the Tiles class is is a declaration telling Java that we want memory space for an object reference of type PImage. This object reference contains all sorts of information including the memory location of theactual pixel data etc but at the moment it references nothing so has the value null.

Now the statement
tiles.imgLoader(tiles.pokeCenter, "pokeCenter");
We are passing the object reference in the first parameter BUT Java will send a copy of the object reference not the original reference object. Now inside the imgLoader the copy is being used to load the image when the method ends the parameters are destroyed so you lose the copy reference object and the original is unchanged so tiles.pokeCenter is still null.

There are other errors for instance you have image(grass, a, b, 10, 10); but you have not declared a PImage variable called grass.

This is not the full answer to all the errors but it might get you started down the right track.

2 Likes

Maybe it’s me that I’m stupid: is it impossible to make a construct that loads a mass set of PImage class objects, passing as arguments of an ipothetical function the PImage object and the name of the image I want to create? Plz explain me if so because I’m not a Java expert but I still need for my project to load a huge quantity of files, so it would be very expensive to make a loadImage(...) line for each of them…

PS referring to the line image(grass, a, b, 10, 10); I want to clarify it was another test I did before and I forgot to delete, but I actually knew it would have caused problems.

public static class Tiles {

  private static HashMap<String, PImage> store = new HashMap<String, PImage>();

  public static void storeImage(PApplet pa, String fname){
    PImage img = pa.loadImage(fname + ".png");
    if(img != null){
      store.put(fname, img);
    }
  }

  public static PImage getImage(String fname){
    return store.get(fname);
  }

}

Assuming you have an image file called “pokemon.png” then to store the image you use
Tiles.storeImage(this, "pokemon");

and to retrieve the image

my_pimage = Tiles.getImage("pokemon");

I wrote this on my tablet so I have not been able to test it for syntax errors.