Hi,
I'm looking for help on how to compare variables which are random and changing inside arrays in two different classes. Not sure if I'm making sense with that so please see below:
I'd like it so that if the xLoc variables below on both the Deer and Car classes are the same, that the Deer's array subtracts by 1. I created a void called "crash", directly following the "void draw", which is hidden out below as an example of what I was trying, but I keep geeting the error " Cannot make a static reference to the non-static field Deer.xLoc". Which makes sense as these floats are constantly changing.
In otherwords if the Deer hits the Car a deer is deleted. - I'm just starting out so not sure how to make this work and any help is appreciated. - Cheers!!
void setup() {
size(800,800);
for (int loop = 0; loop < newDeer.length; loop++){
newDeer [loop] = new Deer(random(0,800),0,20,20,random(1,6));
}
for (int loop = 0; loop < newCar.length; loop++){
newCar [loop] = new Car(0,random(0,800),20,20,random(1,25));
}
}
void draw(){
background(255);
for (int loop = 0; loop < newDeer.length; loop++) {
newDeer[loop].drawDeer();
newDeer[loop].deerMove();
}
for (int loop = 0; loop < newCar.length; loop++) {
newCar[loop].drawCar();
newCar[loop].carMove();
}
}
/* ERROR AND MESSAGE
void crash(){
if (Deer.xLoc == Car.xLoc) {
numberOfDeer = numberofDeer - 1;
}
}
Cannot make a static reference to the non-static field Deer.xLoc
*/
int numberOfDeer = 3;
Deer newDeer[] = new Deer[numberOfDeer];
class Deer{
float xLoc;
float yLoc;
float xDiam;
float yDiam;
float deerMoveBy;
Deer (float tempX, float tempY, float tempXDiam, float tempYDiam, float tempSpeed){
xLoc = tempX;
yLoc = tempY;
xDiam = tempXDiam;
yDiam = tempYDiam;
deerMoveBy = tempSpeed;
}
void drawDeer() {
fill(173,146,10);
ellipse(xLoc,yLoc,xDiam,yDiam);
}
void deerMove () {
yLoc = yLoc + deerMoveBy;
if (yLoc >= height){
yLoc = 0;
}
}
}
int numberOfCars = 15;
Car newCar[] = new Car[numberOfCars];
class Car{
float xLoc;
float yLoc;
float xWidth;
float yHeight;
float carMoveBy;
Car (float tempX, float tempY, float tempWidth, float tempheight, float tempSpeed){
xLoc = tempX;
yLoc = tempY;
xWidth = tempWidth;
yHeight = tempheight;
carMoveBy = tempSpeed;
}
void drawCar() {
fill(219,51,4);
rect(xLoc,yLoc,xWidth,yHeight);
}
void carMove () {
xLoc = xLoc + carMoveBy;
if (xLoc >= width){
xLoc = 0;
}
}
}
I'm looking for help on how to compare variables which are random and changing inside arrays in two different classes. Not sure if I'm making sense with that so please see below:
I'd like it so that if the xLoc variables below on both the Deer and Car classes are the same, that the Deer's array subtracts by 1. I created a void called "crash", directly following the "void draw", which is hidden out below as an example of what I was trying, but I keep geeting the error " Cannot make a static reference to the non-static field Deer.xLoc". Which makes sense as these floats are constantly changing.
In otherwords if the Deer hits the Car a deer is deleted. - I'm just starting out so not sure how to make this work and any help is appreciated. - Cheers!!
void setup() {
size(800,800);
for (int loop = 0; loop < newDeer.length; loop++){
newDeer [loop] = new Deer(random(0,800),0,20,20,random(1,6));
}
for (int loop = 0; loop < newCar.length; loop++){
newCar [loop] = new Car(0,random(0,800),20,20,random(1,25));
}
}
void draw(){
background(255);
for (int loop = 0; loop < newDeer.length; loop++) {
newDeer[loop].drawDeer();
newDeer[loop].deerMove();
}
for (int loop = 0; loop < newCar.length; loop++) {
newCar[loop].drawCar();
newCar[loop].carMove();
}
}
/* ERROR AND MESSAGE
void crash(){
if (Deer.xLoc == Car.xLoc) {
numberOfDeer = numberofDeer - 1;
}
}
Cannot make a static reference to the non-static field Deer.xLoc
*/
int numberOfDeer = 3;
Deer newDeer[] = new Deer[numberOfDeer];
class Deer{
float xLoc;
float yLoc;
float xDiam;
float yDiam;
float deerMoveBy;
Deer (float tempX, float tempY, float tempXDiam, float tempYDiam, float tempSpeed){
xLoc = tempX;
yLoc = tempY;
xDiam = tempXDiam;
yDiam = tempYDiam;
deerMoveBy = tempSpeed;
}
void drawDeer() {
fill(173,146,10);
ellipse(xLoc,yLoc,xDiam,yDiam);
}
void deerMove () {
yLoc = yLoc + deerMoveBy;
if (yLoc >= height){
yLoc = 0;
}
}
}
int numberOfCars = 15;
Car newCar[] = new Car[numberOfCars];
class Car{
float xLoc;
float yLoc;
float xWidth;
float yHeight;
float carMoveBy;
Car (float tempX, float tempY, float tempWidth, float tempheight, float tempSpeed){
xLoc = tempX;
yLoc = tempY;
xWidth = tempWidth;
yHeight = tempheight;
carMoveBy = tempSpeed;
}
void drawCar() {
fill(219,51,4);
rect(xLoc,yLoc,xWidth,yHeight);
}
void carMove () {
xLoc = xLoc + carMoveBy;
if (xLoc >= width){
xLoc = 0;
}
}
}
1