Right now I have three classes from following a tutorial I found online.
It draws a red square and a blue square to the screen and lets the user control the red square.
The issue I am having is getting it so that when they intersect from the bottom of the red and the top of the blue I need it to keep the Y coordinate (Which would basically be the Y coordinate of the blue square minus the height which would bee 100, correct?) and then the X coordinate should not be changed, then I would also need to do the same for intersection with the top of the red with the bottom of the blue, and the same idea for the left and right sides correct? Would there be an easier way of doing this?
public void checkCollisions()
{
if (player.getBounds().intersects(enemy.getBounds()))
player.collision = true;
else
player.collision = false;
}
public void drawBuffer()
{
Graphics2D b = buffer.createGraphics();
b.setColor(Color.black);
b.fillRect(0,0,800,600);
if (player.collision == false)
{
b.setColor(Color.red);
b.fillRect(player.getX(),player.getY(),player.getWidth(),player.getHeight());
b.setColor(Color.blue);
b.fillRect(enemy.getX(),enemy.getY(),enemy.getWidth(),enemy.getHeight());
b.dispose();
}
else if (player.collision == true)
{
b.setColor(Color.red);
b.fillRect(player.getXCollide(),player.getYCollide(),player.getWidth(),player.getHeight());
b.setColor(Color.blue);
b.fillRect(enemy.getX(),enemy.getY(),enemy.getWidth(),enemy.getHeight());
b.dispose();
}
}
That is the code for detecting when they collide and this is what I have tried doing when they intersect.
public int getYCollide()
{
y = y - height;
return y;
}
public int getXCollide()
{
x = x - width;
return x;
}
Now I am having trouble getting it to do the x of the red square (player) subtracting the x of the blue square (enemy) minus the width.
The "getXCollide()" and all the other code relating to those are in a class called entity and the other chunk of code is from DrawPanel.
DrawPanel creates Entity Player and Entity Enemy.
Should I be doing the X and Y subtractions in the DrawPanel class?