Cannot make static reference to non-static error when placing a class at mouseX
in
Programming Questions
•
1 year ago
So I'm developing an ant simulator, and having minor issues with placing a class at mouseXmouseY on mousePressed.
I'm getting the "Cannot make a static reference to the non-static method" error, and was wondering if someone could help me with it. I know its a really simple error to fix, but for some reason, I'm having trouble getting it to place and i'm getting frustrated.
- int antNum = 10; // Set the number of ants
- int faderate = 2; // Set background redraw opacity
- Ant[] antList; // Create a list to hold the ants
- void setup(){
- size(500,500);
- background(255);
- smooth();
- //Make a list of ants
- antList = new Ant[antNum];
- //Make a bunch of ants and put them in the ant list
- for (int i = 0; i < antNum; i ++){
- Ant put = new Ant();
- put.x = width/2;
- put.y = height/2;
- put.speed = 3;
- put.antSize = 5;
- antList[i] = put;
- }
- }
- void draw(){
- fill(255,255,255,faderate); //Rectangle fadeaway
- rect(0,0,width, height);
- for (int i =0; i <antNum; i ++) {
- Ant currentAnt = antList[i];
- currentAnt.walk();
- }
- }
- //Place Food
- void mousePressed() {
- Food.addFood(mouseX,mouseY);
- }
- //Saves a screenshot of the ants
- void keyPressed() {
- if (key== 's'){
- save(month()+":"+day()+":"+year()+"_"+hour()+"."+minute()+"."+second()+"_"+"ants.png");
- }
- }
- class Food {
- float range = 25;
- Food(){
- }
- void addFood(int x, int y){
- //Draw the food
- fill(255,69,0,25);
- noStroke();
- rectMode(CENTER);
- rect(mouseX,mouseY,8,8);
- }
- }
If you wanted the ant class too, just to see how this actually runs:
- class Ant {
- float x;
- float y;
- float speed = 1;
- float antSize = 1;
- Ant(){
- }
- void walk(){
- //Update the position
- x = x + random(-speed, speed);
- y = y + random(-speed,speed);
- //Draw the ant
- fill(0,0,0);
- noStroke();
- ellipse(x,y,antSize/1.5,antSize/1.5);
- //Bounding box
- if (x < 0) {
- x = 0;
- }
- if (y < 0) {
- y = 0;
- }
- if (x > width) {
- x = width;
- }
- if (y > height) {
- y = height;
- }
- }
- }
1