i have a very simple code as I’m tryna learn collision detection as I want to use it in the future.
//Falling Game
Faller player; //declare instance variable
Obstacle thing;
void setup()
{
size(400, 600);
player = new Faller(width/2, 20); //top of screen
thing = new Obstacle(width/2,height, -2); //bottom of screen
}
void draw()
{
background(0);
player.render();
thing.move();
thing.render();
// has the obstacle crashed into the faller instance
// do something
if (thing.crashed(player))
{
player = null; //i get an error here!!
}
}
class Obstacle
{
int x, y, speedY;
//SpeedY is -ve value
Obstacle(int x, int y, int speedY)
{
this.x = x;
this.y = y;
this.speedY = speedY;
}
void render()
{
fill(255, 0, 0); //red
ellipse(this.x, this.y, 30, 30);
}
void move() //moved up screen
{
this.y = this.y + this.speedY;
}
boolean crashed(Faller otherObject) //reports on status of this obstacle
{
if ( abs(otherObject.x - this.x) < 10)
{
if (abs(otherObject.y - this.y) < 10)
{
return true;
}
}
return false;
//return false if no crash with player
//return true if distance to player is very close - faller x,y
}
}//end of class
class Faller
{
int x, y;
Faller(int x, int y)
{
this.x = x;
this.y = y;
}
void render()
{
fill(0,255,0);
rect(x, y, 10, 30);
}
} //end of class
i want to make my object disappear on collision and I thought maybe using =null would solve this for me but it doesnt.
//Falling Game
Faller player; //declare instance variable
Obstacle thing;
void setup()
{
size(400, 600);
player = new Faller(width/2, 20); //top of screen
thing = new Obstacle(width/2, height, -2); //bottom of screen
}
void draw()
{
background(0);
if (player != null)
player.render();
thing.move();
thing.render();
// has the obstacle crashed into the faller instance
// do something
if (thing.crashed(player))
{
player = null; //i get an error here!!
}
}
// =========================================================================================
class Obstacle
{
int x, y, speedY;
//SpeedY is -ve value
Obstacle(int x, int y, int speedY)
{
this.x = x;
this.y = y;
this.speedY = speedY;
}
void render()
{
fill(255, 0, 0); //red
ellipse(this.x, this.y, 30, 30);
}
void move() //moved up screen
{
if (player==null)
return;
this.y = this.y + this.speedY;
}
boolean crashed(Faller otherObject) //reports on status of this obstacle
{
if (otherObject==null)
return false;
if ( abs(otherObject.x - this.x) < 10)
{
if (abs(otherObject.y - this.y) < 10)
{
return true;
}
}
return false;
//return false if no crash with player
//return true if distance to player is very close - faller x,y
}
}//end of class
// =========================================================================================
class Faller
{
int x, y;
Faller(int x, int y)
{
this.x = x;
this.y = y;
}
void render()
{
fill(0, 255, 0);
rect(x, y, 10, 30);
}
} //end of class
//