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

2017/5/2

Need help with movement

xs3ppx xs3ppx

2017/5/2

#
I´m trying to get my actor to move on a line of blocks, but sometimes it skips some of them. Thanks for any help. My code:
public class Ghost extends Actor
{
   String lastMove = null;   
   int x;
   int y;
           
   public void act() 
   {    
     move(); 
   }
     
    public void move()
   { 
       if (getOneObjectAtOffset(0,1,Block.class)!=null)
       {           
           if(lastMove != "up")
           {         
               moveDown();             
            }
        }
 
       if (getOneObjectAtOffset(0,-1,Block.class)!=null)
       {
           if (lastMove != "down")
           {
               moveUp(); 
            }
        }
        
       if (getOneObjectAtOffset(-1,0,Block.class)!=null)
       { 
           if(lastMove != "right")
           {            
               moveLeft();             
           }
       }
   
       if (getOneObjectAtOffset(1,0,Block.class)!=null)
       {     
          if(lastMove != "left")
          {
               moveRight();               
          }
       } 
    }
  
   public void moveDown()
   {
       getCoordiantes();
       setLocation(x,y+1);                            
       lastMove = "down";      
   }
   
   public void moveUp()
   {
       getCoordiantes();
       setLocation(x,y-1);                            
       lastMove = "up";        
   }
   
   public void moveLeft()
   {
       getCoordiantes();      
       setLocation(x-1,y);                               
       lastMove = "left";          
   }
   
   public void moveRight()
   {
       getCoordiantes();        
       setLocation(x+1,y);                             
       lastMove = "right";      
   }
   
   public void getCoordiantes()
   {
       x = getX(); 
       y = getY();       
   }
}
danpost danpost

2017/5/2

#
You could start by putting 'else' at the beginning of lines 22, 30 and 38 so that you not do an action on top of an action.
xs3ppx xs3ppx

2017/5/2

#
It doesn´t work :/, any other ideas?
danpost danpost

2017/5/2

#
xs3ppx wrote...
It doesn´t work :/, any other ideas?
Not currently. Sorry.
danpost danpost

2017/5/3

#
Okay, try changing your String comparisons to this format (example for line 16):
if (!"up".equals(lastMove))
This will compare the characters within the strings to each other. Using '!=' or '==' on String object does not check their character strings. Using them on any set of objects will compare the memory location of each. If both are stored in memory at the same location, then they refer to the same object.
xs3ppx xs3ppx

2017/5/3

#
Found an easier way. Thanks for your help.
 
public void move()
   { 
       if (getOneObjectAtOffset(0,1,Waypoint_Test.class)!=null && lastMove != "up")
       {                  
           moveDown();             
       }
       else if (getOneObjectAtOffset(0,-1,Waypoint_Test.class)!=null && lastMove != "down")
       {
           moveUp(); 
       }
       else if (getOneObjectAtOffset(-1,0,Waypoint_Test.class)!=null && lastMove != "right")
       {          
           moveLeft();             
       }
       else if (getOneObjectAtOffset(1,0,Waypoint_Test.class)!=null && lastMove != "left")
       {     
           moveRight();               
       } 
    }
You need to login to post a reply.