import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Lad here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lad extends Actor
{
/**
* Act - do whatever the Lad wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkkey();
}
public void checkkey()
{
if (Greenfoot.isKeyDown("d") && ("space".equals(Greenfoot.getKey())))
{setImage ("rightlad.gif");
shootright();
moveright();
}
else if(Greenfoot.isKeyDown("d"))
{
setImage("rightlad.gif");
moveright();
}
{
if (Greenfoot.isKeyDown("a") && ("space".equals(Greenfoot.getKey())))
{setImage ("leftlad.gif");
shootleft();
if(getOneObjectInFront(wall.class)==null)
moveleft();
}
else if(Greenfoot.isKeyDown("a"))
{
setImage("leftlad.gif");
if(getOneObjectInFront(wall.class)==null)
moveleft();
}
if (Greenfoot.isKeyDown("w") && ("space".equals(Greenfoot.getKey())))
{setImage ("backlad.gif");
shootup();
moveup();
}
else if(Greenfoot.isKeyDown("w"))
{
setImage("backlad.gif");
moveup();
}
if (Greenfoot.isKeyDown("s") && ("space".equals(Greenfoot.getKey())))
{setImage ("forwardslad.gif");
shootdown();
if(getOneObjectInFront(wall.class)==null)
movedown();
}
else if(Greenfoot.isKeyDown("s"))
{
setImage("forwardslad.gif");
if(getOneObjectInFront(wall.class)==null)
movedown();
}
}
}
public void moveright()
{
setLocation(getX()+5, getY());
}
public void moveleft()
{
setLocation(getX()-5, getY());
}
public void moveup()
{
setLocation(getX(), getY()-5);
}
public void movedown()
{
setLocation(getX(), getY()+5);
}
public void shootleft()
{
getWorld() .addObject(new bulletleft(), getX(), getY());
}
public void shootright()
{
getWorld() .addObject(new bulletright(), getX(), getY());
}
public void shootup()
{
getWorld() .addObject(new bulletup(), getX(), getY());
}
public void shootdown()
{
getWorld() .addObject(new bulletdown(), getX(), getY());
}
private Actor getOneObjectInFront(Class wall)
{
GreenfootImage myImage = getImage();
int distanceToFront = myImage.getWidth();
int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation())));
int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation())));
return (getOneObjectAtOffset(xOffset, yOffset, wall));
}
}
when my game runs i want this code to make the player not be able to move through walls
there isnt an error but instead just doesnt work as i want it to
any help would be appreciated, thank you for your time!


