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?
if (isTouching(wall.class))
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 downif (dx == 0 && dy == 0) return;
animate(dx, dy);
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;
}
}setLocation(getX()+dx*3, getY()+dy*3);
if (isTouching(Walls.class)) setLocation(getX()-dx*3, getY()-dy*3);