void player() {
public class player {
private int dx;
private int dy;
private int x = 40;
private int y = 60;
private int w;
private int h;
private Image image;
public player() {
loadImage();
}
private void loadImage() {
ImageIcon ii = new ImageIcon("player.png");
image = ii.getImage();
w = image.getWidth(null);
h = image.getHeight(null);
}
public void move() {
x += dx;
y += dy;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return w;
}
public int getHeight() {
return h;
}
public Image getImage() {
return image;
}
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = -2;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 2;
}
if (key == KeyEvent.VK_UP) {
dy = -2;
}
if (key == KeyEvent.VK_DOWN) {
dy = 2;
}
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) {
dx = 0;
}
if (key == KeyEvent.VK_RIGHT) {
dx = 0;
}
if (key == KeyEvent.VK_UP) {
dy = 0;
}
if (key == KeyEvent.VK_DOWN) {
dy = 0;
}
}
}
}
The class must not be inside a function
Tysm so you have to delete void player() {}
right?
Yes. Or close this function before the class starts. Use }
bracket
There is a nice text tutorial about objects and classes
Remarks
You also need setup()
and draw()
outside of the class.
Are you working in the processing IDE?
Data type is PImage, not Image.
The data type is Image
because the Image is obtained fron Java AWT or Swing
Couple of other points regarding the original code
In Java it is normal to capitalise the class name so use Player
, this helps others to identify your user defined classes.
Generally I would avoid using Processing function/variable names in your class. loadImage
is a Processing function that loads a bitmap file into a PImage
object hence the confusion you caused for @Chrisir