I have been trying to create a simple collision detection with a bounding box
So far I have got the X collision to work but it only works when moving to the right and when moving to the left it detects to the left of the x coordinate of the object
int speed = 5;
int value = 0;
public void act()
{
checkKeys();
}
public void checkKeys()
{
if (Greenfoot.isKeyDown("w"))
{
setLocation(getX(), getY() - speed);
}
if (Greenfoot.isKeyDown("s"))
{
setLocation(getX(), getY() + speed);
}
if (Greenfoot.isKeyDown("a"))
{
if (collisionDetection() != 2)
{
setLocation(getX() - speed, getY());
}
}
if (Greenfoot.isKeyDown("d"))
{
if (collisionDetection() != 1)
{
setLocation(getX() + speed, getY());
}
}
}
public int collisionDetection()
{
value = 0;
if (isTouching(null))
{
Actor touching = getOneIntersectingObject(barrel.class);
if ( (getX() + getImage().getWidth()) >= touching.getX())
{
value = 1;
}
else if ( (getX()) <= (touching.getX() + 50))
{
value = 2;
}
else
{
value = 0;
}
}
return value;
}
}

