Right now I am working on hit detection in Greenfoot and have 3 out of the 4 sides of the wall done. I can't seem to get the last one because when I mess with the right wall code the left wall stops working a vice versa. here is my pacman code:
here is my wall code:
and here is the world code:
It seems that the left side is working (from my angle) but the right is setting the location on the other side but when I change the right side code the left stops working.
Thanks
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Pacman here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Pacman extends Actor
{
/**
* Act - do whatever the Pacman wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public Pacman()
{
GreenfootImage image = getImage();
image.scale(35, 35);
setImage(image);
}
public void act()
{
controls();
move(-3);
eat();
checkRightWalls();
platformAbove();
checkLeftWalls();
platformBelow();
}
private void controls()
{
if ( Greenfoot.isKeyDown("right") || Greenfoot.isKeyDown("D") ) {
setRotation(-180);
}
if ( Greenfoot.isKeyDown("left") || Greenfoot.isKeyDown("A") ) {
setRotation(-0);
}
if ( Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("W") ) {
setRotation(-270);
}
if ( Greenfoot.isKeyDown("down") || Greenfoot.isKeyDown("S") ) {
setRotation(-90);
}
}
private boolean platformAbove()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int) (spriteHeight / -2 );
Actor ceiling = getOneObjectAtOffset(0, yDistance, Wall.class);
if(ceiling != null)
{
bopHead(ceiling);
return true;
}
else
{
return false;
}
}
public void bopHead (Actor ceiling)
{
int ceilingHeight = ceiling.getImage().getHeight();
int newY = ceiling.getY() +(ceilingHeight + getImage().getWidth())/2;
setLocation(getX() , newY);
}
private boolean platformBelow()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int) (spriteHeight / 2 );
Actor floor = getOneObjectAtOffset(0, yDistance, Wall.class);
if(floor != null)
{
bopHeads(floor);
return true;
}
else
{
return false;
}
}
public void bopHeads (Actor floor)
{
int ceilingHeight = floor.getImage().getHeight();
int newYL = floor.getY() -(ceilingHeight + getImage().getWidth())/2;
setLocation(getX() , newYL);
}
private boolean checkRightWalls()
{
int spritewidth = getImage().getWidth();
int xDistance = (int)(spritewidth/2);
Actor rightwall = getOneObjectAtOffset(xDistance, 0, Wall.class);
if(rightwall == null)
{
return false;
}
else
{
stopByRightWall(rightwall);
return true;
}
}
public void stopByRightWall (Actor rightwall)
{
int wallWidth = rightwall.getImage().getWidth();
int newX = rightwall.getX() -(wallWidth + getImage().getWidth())/2;
setLocation(newX , getY());
}
private boolean checkLeftWalls()
{
int spritewidth = getImage().getWidth();
int xDistance = (int)(spritewidth/2);
Actor leftwall = getOneObjectAtOffset(xDistance, 0, Wall.class);
if(leftwall == null)
{
return false;
}
else
{
stopByLeftWall(leftwall);
return true;
}
}
public void stopByLeftWall (Actor leftwall)
{
int wallWidth = leftwall.getImage().getWidth();
int newXL = leftwall.getX() -(wallWidth + getImage().getWidth())/2;
setLocation(newXL , getY());
}
public void eat()
{
Actor Pellet = getOneObjectAtOffset(0, 0,Pellet.class);
if(Pellet != null)
{
World world = getWorld();
world.removeObject(Pellet);
((MyWorld)world).counter.add(1);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Wall here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Wall extends Actor
{
/**
* Act - do whatever the Wall wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
}
public Wall()
{
GreenfootImage image = getImage();
image.scale(60, 40);
setImage(image);
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends greenfoot.World
{
Counter counter;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
prepare();
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Pacman pacman = new Pacman();
addObject(pacman,780,100);
counter = new Counter();
addObject(counter,728,26);
Wall wall = new Wall();
addObject(wall,770,66);
Wall wall2 = new Wall();
addObject(wall2,710,66);
Wall wall3 = new Wall();
addObject(wall3,650,66);
Wall wall4 = new Wall();
addObject(wall4,590,66);
}
}
