Character
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Dom here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Dom extends Mover
{
private int speed=4;
public Dom()
{
}
public void act()
{
checkKeys();
Actor actor = getOneIntersectingObject(Dom.class);
if(actor != null)
{
getWorld().removeObject(this);
}
}
private void checkKeys(){
if(Greenfoot.isKeyDown("left")){
moveLeft();
}
if(Greenfoot.isKeyDown("right")){
moveRight();
}
if(Greenfoot.isKeyDown("up")){
moveUp();
}
if(Greenfoot.isKeyDown("down")){
moveDown();
}
}
public void moveRight(){
setLocation(getX()+speed,getY());
}
public void moveLeft(){
setLocation(getX()-speed,getY());
}
public void moveUp(){
setLocation(getX(),getY() -speed);
}
public void moveDown(){
setLocation(getX(),getY() +speed);
}
}
Cave_rotated
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Cave_rotated here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Cave_rotated extends Actor
{
/**
* Act - do whatever the Cave_rotated wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
}
Cave
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Cave here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Cave extends Actor
{
/**
* Act - do whatever the Cave wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
}
}
If your walls are at least 4 pixels wide then you should be able to detect when you came upon one. After the 'checkKeys' call in the act method, add a 'checkWalls' method. Create the new 'checkWalls' method checking for intersecting walls (one check for each type of wall). If either wall is present, move the character back to the location it started at. You will have to add fields to hold the initial location and set them at the beginning of the act method.