So I'm having so problems, I have a ball and there are some walls blocking the ball so obviously it can't move in the direction where the walls are
But say there's a space available below, how can i get this to automatically move down?
public boolean canMove(int x, int y)
{
Actor sand;
sand=getOneObjectAtOffset(x,y,sandroad.class);
//the section below checks if there is a block you can move to
// if there is it sets sand to a vlaue otherwise it says null
// The errors are in this section
boolean flag=true;
if (sand ==null)
{
flag=false;
}
return flag;
}
public void key()
{
//Note 1: Down the page increase the y value and going to the right increases the x value
//Note 2: Each block is 60 pixels wide and high
int leftChange=-60;
int rightChange=60;
int upChange=-60;
int downChange=60;
String key = Greenfoot.getKey();
if (key != null)
{
if (key.equals("left"))
{
if (canMove(leftChange, 0)==true){
setLocation(getX()+leftChange, getY()) ;}
}
if (key.equals("right"))
{
if (canMove(rightChange, 0)==true){
setLocation(getX()+rightChange, getY()) ;}
}
if (key.equals("up"))
{
if (canMove(0, upChange)==true){
setLocation(getX(), getY()+upChange) ;}
}
if (key.equals("down"))
{
if (canMove(0, downChange)==true){
setLocation(getX(), getY()+downChange) ;}
}
}
}
public void win()
{
Actor win;
win=getOneObjectAtOffset(0,0,Goal.class);
if (win !=null)
{
Greenfoot.stop();
}
public boolean canMove(int x, int y)
{
return getOneObjectAtOffset(x, y, sandroad.class).isEmpty();
}