This site requires JavaScript, please enable it in your browser!
Greenfoot back
woodentable
woodentable wrote ...

2018/1/6

objects moves down when space is available?

woodentable woodentable

2018/1/6

#
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?
xbLank xbLank

2018/1/6

#
So you want to move the walls that are blocking the wall down when there is space below?
woodentable woodentable

2018/1/6

#
Sorry it's a maze, so when there is a space for the ball to move down, it automatically drops down to the next section
xbLank xbLank

2018/1/6

#
Ahhh. What have you got so far?
woodentable woodentable

2018/1/6

#
so far
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();
        }

appreciate the help
xbLank xbLank

2018/1/6

#
Use the getOneObjectAtOffset Method in a constant check depending on the balls current rotation to check if there is a wall blocking your way. If thats not the case, move the ball in that direction. I apologize that I can't offer you code because I am actually busy right now but I hope I was able to give you a little hint.
Super_Hippo Super_Hippo

2018/1/6

#
I guess you could add lines 47 and 48 at 51 again. Btw, you can simplify the canMove method using this:
public boolean canMove(int x, int y)
{
    return getOneObjectAtOffset(x, y, sandroad.class).isEmpty();
}
woodentable woodentable

2018/1/6

#
Cant thank you guys enough! Appreciate the help, all sorted
xbLank xbLank

2018/1/6

#
Glad to hear that.
You need to login to post a reply.