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?

This site requires JavaScript, please enable it in your browser!

HakimNait wrote...

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))

How can I ensure that my character does not pass through walls? Here I used a hitWall method, but sometimes it's buggy and it passes through.
public void act()
{
mover();
}
public void mover()
{
animationTimer = animationTimer +1;
if(Greenfoot.isKeyDown("d"))
{
setLocation(getX()+2,getY());
if(animationTimer % 8 == 0)
animateRight();
if (hitWalls() == true)
{
setLocation(getX() - 3,getY());
}
}
if(Greenfoot.isKeyDown("a"))
{
setLocation(getX()-2,getY());
if(animationTimer % 8 == 0)
animateLeft();
if (hitWalls() == true)
{
setLocation(getX() + 3,getY());
}
}
if(Greenfoot.isKeyDown("w"))
{
setLocation(getX(),getY()-2);
if(animationTimer % 8 == 0)
animateUp();
if (hitWalls() == true)
{
setLocation(getX(),getY() + 3);
}
}
if(Greenfoot.isKeyDown("s"))
{
setLocation(getX(), getY()+2);
if(animationTimer % 8 == 0)
animateDown();
if (hitWalls() == true)
{
setLocation(getX(),getY() - 3);
}
}
if(Greenfoot.isKeyDown("d") &&Greenfoot.isKeyDown("s"))
{
setLocation(getX(),getY());
if(animationTimer % 8 == 0)
animateDownRight();
if (hitWalls() == true)
{
setLocation(getX() - 3,getY() - 3);
}
}
if(Greenfoot.isKeyDown("a") &&Greenfoot.isKeyDown("s"))
{
setLocation(getX(),getY());
if(animationTimer % 8 == 0)
animateDownLeft();
if (hitWalls() == true)
{
setLocation(getX() + 3,getY() - 3);
}
}
if(Greenfoot.isKeyDown("a") &&Greenfoot.isKeyDown("w"))
{
setLocation(getX(),getY());
if(animationTimer % 8 == 0)
animateUpLeft();
if (hitWalls() == true)
{
setLocation(getX() + 3,getY() + 3);
}
}
if(Greenfoot.isKeyDown("d") &&Greenfoot.isKeyDown("w"))
{
setLocation(getX(),getY());
if(animationTimer % 8 == 0)
animateUpRight();
if (hitWalls() == true)
{
setLocation(getX() - 3,getY() + 3);
}
}
}
public boolean hitWalls()
{
if (isTouching(Walls.class))
{
return true;
}
else
{
return false;
}
}

HakimNait wrote...

How can I ensure that my character does not pass through walls? Here I used a hitWall method, but sometimes it's buggy and it passes through.
<< Code Omitted >>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

if (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);

You need to login to post a reply.