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

2017/4/16

How to get a character to move with another moving platform

1
2
danpost danpost

2017/4/16

#
sinlessclown wrote...
How do I make it so that the moving block moves to the right wall first?
Set the initial value of 'direction' to a positive number and place the platform left of the right wall. Also, change line 10:
// from
move(direction);
// to
setLocation(getX()+direction, getY());
sinlessclown sinlessclown

2017/4/16

#
So what do I do to keep the moving blocks on the right half of the world. So that the moving blocks move from the middle of the right half, to the end of the right half, to the middle and back to the end of the right half.
sinlessclown sinlessclown

2017/4/16

#
This is my code so far.
public class Moving_Block extends Platform
{
    private int direction = 2;   
    public void act() 
    { 
        move(); 
        moveEverythingOn();
    }
 
    private void move() 
    {  
        if (direction < 0 && getX() <= getImage().getWidth() / 2
        || direction > 0 && getX() >= getWorld().getWidth() - getImage().getWidth() / 2) {
        direction = -direction;
        }
        setLocation(getX()+direction, getY());
    }
      
   private void moveEverythingOn()
   {
      setLocation(getX(), getY()-1);
      Actor player = getOneIntersectingObject(Player.class);
      if (player != null) player.setLocation(player.getX()+direction, player.getY());
      setLocation(getX(), getY()+1);
   }    
}  
danpost danpost

2017/4/16

#
sinlessclown wrote...
So what do I do to keep the moving blocks on the right half of the world. So that the moving blocks move from the middle of the right half, to the end of the right half, to the middle and back to the end of the right half.
public class Moving_Block extends Platform
{
    private int direction = 2;   

    public void act() 
    { 
        move(); 
    }
  
    private void move() 
    {
        int width = getWorld().getWidth();
        int wide = getImage().getWidth();
        if (direction < 0 && getX() <= (width + wide) / 2 || direction > 0 && getX() >= width - 1 - wide / 2) direction = -direction;
        setLocation(getX()+direction, getY()-1); // moving with shift up
        Actor player = getOneIntersectingObject(Player.class);
        if (player != null) player.setLocation(player.getX()+direction, player.getY());
        setLocation(getX(), getY()+1); // shifting back down
   }    
}
Yehuda Yehuda

2017/4/16

#
@danpost Why do you need the '- 1' on line 14? (You seem to have a lot of trailing spaces on your lines of code.)
danpost danpost

2017/4/16

#
Yehuda wrote...
@danpost Why do you need the '- 1' on line 14? (You seem to have a lot of trailing spaces on your lines of code.)
I copied and modified the previous code given (as far as trailing spaces are concerned). The '-1' is not absolutely necessary, but will prevent the last column of pixels in the image of the player from being off the right edge of the world.
You need to login to post a reply.
1
2