I need help at my beginner idle game

Hey there,
iam a beginner at coding and i wanted to code a clicker game but then this text showed up in the console:

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at processing.core.PApplet.runSketch(PApplet.java:10852)
at processing.core.PApplet.main(PApplet.java:10620)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at processing.core.PApplet.runSketch(PApplet.java:10846)
… 1 more
Caused by: java.lang.NullPointerException
at processing.core.PApplet.rect(PApplet.java:12143)
at Money.(Money.java:50)
… 6 more
RuntimeException: java.lang.reflect.InvocationTargetException

It is easier if you post the code. It makes it far faster too and since almost nothing is visible from the error message we can’t help you

okay:

int money;
PImage pic1;
PImage pic2;
int rx1 = 250;
int ry1 = 200;
int rwidth1 = 280;
int rheight1 = 140;
int rx2 = 450;
int ry2 = 400;
int rwidth2 = 480;
int rheight2 = 440;

void setup() {

size(800,600);

money = 0;

pic1 = loadImage(“foto_1.png”);
pic2 = loadImage(“foto_2.png”);

noStroke();
}
void mousePressed() {
if (rx1 <= mouseX && mouseX <= rx1 + rwidth1 &&
ry1 <= mouseY && mouseY <= ry1 + rheight1) {
money = money + 1;
}
}
{if (rx2 <= mouseX && mouseX <= rx2 + rwidth2 &&
ry2 <= mouseY && mouseY <= ry2 + rheight2) {
fill(200,0,0);
}
rect(rx2,ry2,rwidth2, rheight2);
}

void draw() {
background(200);
println(money);
textSize(32);
fill(0);
image(pic1, 100,15);
pic1.resize(0, 80);
image(pic2, 250,200);
text(money , 50,60);
}

Hi @Dialias

The problem is in your mousePressed function(). You have an extra parentesis
Try substituting by:

void mousePressed() {
  if (rx1 <= mouseX && mouseX <= rx1 + rwidth1 &&
    ry1 <= mouseY && mouseY <= ry1 + rheight1) {
    money = money + 1;
  }

  if (rx2 <= mouseX && mouseX <= rx2 + rwidth2 &&
    ry2 <= mouseY && mouseY <= ry2 + rheight2) {
    fill(200, 0, 0);
  }
  rect(rx2, ry2, rwidth2, rheight2);
}

You can format your code in the PDE to easily catch this time of errors: Edit -> Auto format on the PDE tabs (in windows the shortcut is CTRL+T)

Best regards,

1 Like

thank you ! ༼ つ ◕_◕ ༽つ

I looked into this and it occurred to me that the rect appears only briefly

Using a boolean variable I made it so that the rect appears for 1.2 seconds



int money;
PImage pic1;
PImage pic2;
int rx1 = 250;
int ry1 = 200;
int rwidth1 = 280;
int rheight1 = 140;
int rx2 = 450;
int ry2 = 400;
int rwidth2 = 480;
int rheight2 = 440;

boolean showRect=false; 
int timer; 

// ------------------------------------------------------------

void setup() {

  size(800, 600);

  money = 0;

  pic1 = loadImage("foto_1.png");
  pic2 = loadImage("foto_2.png");

  noStroke();
}

void draw() {
  background(200);
  println(money);
  textSize(32);
  fill(0);
  text("pic1", 100, 15);
  // pic1.resize(0, 80);
  text("pic2", 250, 200);
  text(money, 50, 60);

  if (showRect) {
    fill(200, 0, 0);
    rect(rx2, ry2, rwidth2, rheight2);
    if (millis()-timer > 1200) {
      showRect=false;
    }
  }
}

// ------------------------------------------------------------

void mousePressed() {
  if (rx1 <= mouseX && 
    mouseX <= rx1 + rwidth1 &&
    ry1 <= mouseY && 
    mouseY <= ry1 + rheight1) {
    money = money + 1;
  }


  if (rx2 <= mouseX &&
    mouseX <= rx2 + rwidth2 &&
    ry2 <= mouseY &&
    mouseY <= ry2 + rheight2) {
  }

  showRect=true;
  timer=millis();
}
//

1 Like

thanks for sharing! I modified this code make something even cool - a cps counter. It basically counts the click (like you did) and then added a functionality show rank as per the player’s score. I want help in adding one more feature to save historical scores of the users. Can anyone share something similar?

1 Like