I would like to create collisions in my games all in a super class: wall for any image that I enter there have collisions, but I don't know how to do it because I'm a beginner.
can someone help?


1 | if (isTouching(wall. class )) |
1 2 3 4 5 | int dx = 0 , dy = 0 ; // to hold key down information for both horizontal and vertical directions if (Greenfoot.isKeyDown( "d" )) dx++; // right (d) key down if (Greenfoot.isKeyDown( "a" )) dx--; // left (a) key down if (Greenfoot.isKeyDown( "s" )) dy++; // down (s) key down if (Greenfoot.isKeyDown( "w" )) dy--; // up (w) key down |
1 | if (dx == 0 && dy == 0 ) return ; |
1 | animate(dx, dy); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private void animate( int dx, int dy) { animationTimer = ( 1 +animationTimer)% 8 ; if (animationTimer% 8 != 0 ) return ; int dir = 0 ; if (dx == 0 ) dir = ( 2 -dy)* 2 ; else if (dy == 0 ) dir = ( 1 -dx)* 2 ; else dir = ( 2 -dy)* 2 -dx*dy; switch (dir) { case 0 : animateRight(); break ; case 1 : animateDownRight(); break ; case 2 : animateDown(); break ; case 3 : animateDownLeft(); break ; case 4 : animateLeft(); break ; case 5 : animateUpLeft(); break ; case 6 : animateUp(); break ; case 7 : animateUpRight(); break ; } } |
1 | setLocation(getX()+dx* 3 , getY()+dy* 3 ); |
1 | if (isTouching(Walls. class )) setLocation(getX()-dx* 3 , getY()-dy* 3 ); |