I'm trying to make a game for the class I'm in and I'm having trouble preventing the player from passing through a wall without stopping the game. I'd like it to get to the wall, not be able to move through it and lose a life. I know I can subtract from the X or Y value but it would have to depend on the location of the player. Here is my code:
public void move()
{
if (Greenfoot.isKeyDown("up")) //Move UP one "square"
{
setLocation(getX(), getY() - 3);
walkFrame++;
if(walkFrame == 15){
setImage(walk2);
}
else if(walkFrame == 30 ){
setImage(walk1);
walkFrame = 0;
}
}
if (Greenfoot.isKeyDown("down")) //Move down one "square"
{
setLocation(getX(), getY() + 3);
walkFrame++;
if(walkFrame == 15){
setImage(walk2);
}
else if(walkFrame == 30 ){
setImage(walk1);
walkFrame = 0;
}
}
if (Greenfoot.isKeyDown("right")) //Move right one "square"
{
setLocation(getX() + 3, getY());
walkFrame++;
if(walkFrame == 15){
setImage(walk2);
}
else if(walkFrame == 30 ){
setImage(walk1);
walkFrame = 0;
}
}
if (Greenfoot.isKeyDown("left")) //Move left one "square"
{
setLocation(getX() - 3, getY());
walkFrame++;
if(walkFrame == 15){
setImage(walk2);
}
else if(walkFrame == 30 ){
setImage(walk1);
walkFrame = 0;
}
}
}
public void checkCollision(){
int originalX = getX();
int originalY = getY();
if(getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(Block.class) != null){
// where i plan to place the move back code
Greenfoot.playSound("hurt.wav");
lives--;
}
}
