We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I'm making a game about salmon and when I try to add collision code, I get the error "cannot make static reference to non-static field Bass.x" and I've tried many different things. Please help! [-O<
Here is the code:
PImage pic;
PImage pic2;
PImage pic3;
PImage pic4;
PImage pic5;
PImage pic6;
PImage pic7;
PImage pic8;
PFont menuFont;
PFont animFont;
float imgX = 100;
float imgY = 750;
float eggX = 500;
float eggY = 700;
float opacity = 0;
float cloud1 = 100;
float cloud2 = 300;
float cloud3 = 500;
float cloud4 = 700;
float cloud5 = 900;
float anim = 5;
float animTimer = 0;
float animTimer2 = 0;
float cloudContinue = 1200;
boolean backgroundIsWhite = false;
Bass[] b = new Bass[2];
AquaticBug[] a = new AquaticBug[3];
Mayfly[] m = new Mayfly[3];
Plankton[] p = new Plankton[4];
String scene = "level1";
void setup() {
size(1000, 1000);
frameRate(100);
imageMode(CENTER);
pic = loadImage("chinook-salmon-alevin.png");
pic2 = loadImage("491375864 (1).png");
pic7 = loadImage("2921797_orig.gif");
menuFont = loadFont("TwCenMT-CondensedBold-100.vlw");
animFont = loadFont("TwCenMT-CondensedBold-48.vlw");
for (int i = 0; i < b.length; i++) {
b[i] = new Bass();
}
for (int i = 0; i < a.length; i++) {
a[i] = new AquaticBug();
}
for (int i = 0; i < m.length; i++) {
m[i] = new Mayfly();
}
for (int i = 0; i < p.length; i++) {
p[i] = new Plankton();
}
}
void useCloud() {
drawCloud(cloud1, 100);
drawCloud(cloud2, 300);
drawCloud(cloud3, 100);
drawCloud(cloud4, 300);
if (cloud1 < -200) {
cloud1 = cloudContinue;
}
if (cloud2 < -200) {
cloud2 = cloudContinue;
}
if (cloud3 < -200) {
cloud3 = cloudContinue;
}
if (cloud4 < -200) {
cloud4 = cloudContinue;
}
if (cloud5 < -200) {
cloud5 = cloudContinue;
}
}
void drawCloud(float a, float b) {
float one = a; // 100
float two = a + 50;
float four = a + 100;
float six = 100;
float seven = b; // 100
float nine = b - 25;
float eleven = b + 10;
noStroke();
fill(255, 255, 255);
ellipse(one, seven, six, six);
ellipse(two, nine, six, six);
ellipse(four, seven, six, six);
ellipse(two, eleven, six, six);
}
void drawSea() {
fill(0, 100, 255);
rect(0, 500, 1100, 550);
fill(78, 79, 75);
rect(0, 900, 1500, 100);
}
void draw() {
switch(scene) {
case "menu" :
{
noStroke();
fill(100, 149, 237);
triangle(0, 0, 0, 1000, 1000, 1000);
fill(70, 130, 180);
triangle(0, 0, 1000, 0, 1000, 1000);
textFont(menuFont);
fill(0, 0, 0);
text("A Salmon's Life", 250, 250);
image(pic2, 350, 300, 306, 190);
fill(0, 102, 204);
rect(400, 500, 200, 100, 200);
rect(400, 650, 200, 100, 200);
rect(400, 800, 200, 100, 200);
break;
}
case "how to" :
{
}
case "info" :
{
}
case "animation" :
{
animTimer2 += 1;
fill(0, 100, 255);
rect(0, 0, 10000, 10000);
noStroke();
fill(78, 79, 75);
rect(0, 700, 1000, 300);
fill(255, 153, 153);
ellipse(eggX, eggY, 100, 100);
textFont(menuFont);
fill(0, 0, 0);
text("A salmon is hatching!", 150, 200);
eggX += 0.5;
eggY += 0.5;
animTimer += 5;
if (eggX >= 501) {
eggX -= 4;
}
if (eggY >= 701) {
eggY -= 3;
}
if (animTimer >= 500) {
backgroundIsWhite = true;
}
if (backgroundIsWhite == true) {
fill(255, 255, 255, opacity);
rect(0, 0, 10000, 10000);
opacity += 5;
}
if (animTimer >= 1000) {
backgroundIsWhite = false;
fill(0, 100, 255);
rect(0, 0, 10000, 10000);
noStroke();
fill(78, 79, 75);
rect(0, 700, 1000, 300);
fill(255, 153, 153);
textSize(20);
textFont(animFont);
fill(0, 0, 0);
text("A salmon has hatched! A baby salmon is called an alevin!", 30, 200);
image(pic, 350, 550, 250, 125);
}
if (animTimer2 >= 600) {
scene = "level1";
}
break;
}
case "level1" :
{
background(135, 206, 235);
drawSea();
if (keyCode == DOWN && keyPressed == true) {
imgY += anim;
}
if (keyCode == UP && keyPressed == true) {
imgY -= anim;
}
useCloud();
image(pic, imgX, constrain(imgY, 550, height - 100));
cloud1 -= 2;
cloud2 -= 2;
cloud3 -= 2;
cloud4 -= 2;
cloud5 -= 2;
for (int i = 0; i < b.length; i++) {
b[i].bass();
b[i].move();
}
for (int i = 0; i < a.length; i++) {
a[i].aquaticBug();
a[i].move();
}
if (dist(imgX+50, imgY, Bass.x-175, Bass.y) <= 0) { // main problem is here!!!
background(255);
}
break;
}
case "level2" :
{
background(135, 206, 235);
drawSea();
if (keyCode == DOWN && keyPressed == true) {
imgY += anim;
}
if (keyCode == UP && keyPressed == true) {
imgY -= anim;
}
useCloud();
image(pic7, imgX, constrain(imgY, 500, height - 100), 150, 50);
cloud1 -= 2;
cloud2 -= 2;
cloud3 -= 2;
cloud4 -= 2;
cloud5 -= 2;
for (int i = 0; i < m.length; i++) {
m[i].mayfly();
m[i].move();
}
for (int i = 0; i < p.length; i++) {
p[i].plankton();
p[i].move();
}
}
}
}
The following classes are all in their own tabs.
class AquaticBug {
float a = random(1000, 5000);
float b = random(500, 900);
float x = a;
float y = b;
float Xspeed = 2;
void move() {
x = x - Xspeed;
if (x < -300) {
x = random(1000, 5000);
;
}
}
void aquaticBug() {
pic4 = loadImage("Moomans hellgrammite.gif");
image(pic4, x, y);
}
}
class Bass {
float x = random(1000, 5000);
float y = random(500, 900);
float Xspeed = 3;
void move() {
x = x - Xspeed;
if (x < -300) {
x = random(1000, 5000);
;
}
}
void bass() {
pic5 = loadImage("LargemouthBass.gif");
image(pic5, x, y);
}
}
class Mayfly {
float x = random(1000, 5000);
float y = random(500, 900);
float Xspeed = 2;
void move() {
x = x - Xspeed;
if (x < -300) {
x = random(1000, 5000);
;
}
}
void mayfly() {
pic6 = loadImage("2017-hafele-spinner-3-1110.gif");
image(pic6, x, y);
}
}
class Plankton {
float x = random(1000, 5000);
float y = random(500, 900);
float Xspeed = 3;
void move() {
x = x - Xspeed;
if (x < -300) {
x = random(1000, 5000);
;
}
}
void plankton() {
pic8 = loadImage("plankton-md.gif");
image(pic8, x, y);
}
}
Answers
A static variable in a class is shared by all objects of that class's type.
Bass.x
would be accessing this single variable - the one variable that all objects of the class share.But this is not what you want.
What you want is that each Bass object has its own x variable.
So you should not make the variable static.
But this means that you do not want to access the static class variable
Bass.x
at all!Instead, you want to access the x variable of a SPECIFIC Bass object.
You have two Bass objects in your code. They are both stored in the
b
array.Their x variables are
b[0].x
andb[1].x
. See the difference? This is getting the x variable for a specific instance of the class.Would the same be for the
y
variable?Also, could you help me with the
dist
function? I am having quite a bit of difficulty with that. Thanks! :DThis is the line with all the problems, right?
What did you think this line is doing? What did you want it to do? Now you know that
Bass.x
is different fromb[0].x
(one is a variable shared by every object of that class's type, the other is a variable that each object of the class has its own copy of), what does this line ACTUALLY do? How does that differ from what you want it to do?I want the line of code to sense when the Bass hits the fish and if it does, then do something. But I think something about the coordinates is off. I'm not sure what though. Is there a better way than
dist
?Okay. Which Bass?
Remember: You have TWO Bass objects. One of them is
b[0]
. The other isb[1]
.If you want the distance between a fish (which I assume is at
(imgX+50, imgY)
) and a Bass - which Bass are you trying to find the distance too?b[0]
?b[1]
? Both of them (in which case you might want to loop over all the Bass objects in your array)?Also,
dist()
will only return 0 (and thus make the<= 0
condition true) if they are the exact same point. Is this really what you want? Maybe you could throw a little leeway in there? How close is close enough?I want some leeway so the if the fish touches anywhere in the Bass, you lose. And yes, I want to check both objects. Could you give me some edited code??? Thanks!
No! Attempt it yourself! Post the code of your attempt and we'll fix it!
Alright. Now your for loop needs an upper limit. The
i
variable is the index of the object in theb
array. The first object in that array has index 0. What index does the last object have? How is that related to the length of the array, AKAb.length
?Next, you are finally using the position of a specific object! YAY! But you are always using the first object in the array -
b[0]
. You want to check distances to all the objects in the array. So instead of always using the index of zero, you will need to use a value that changes each time the loop runs. What's that vari
able?And also... What happens when a collision occurs?
I tried the code and it works!!