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.