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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | 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; } } |