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

2012/5/11

Moving Platform

dark_sky dark_sky

2012/5/11

#
Hello everyone, for my little platforming game, I need moving platforms. Could anyone suggest me a way of implementing them? What is the best way of letting them move between two x-coordinates?
erdelf erdelf

2012/5/11

#
maximumX is the right coordinate, and minimumX the left coordinate. this code is not tested.
private boolean movingRight;
private int maximumX;
private int minimumX;

public void moving platform(int Xa, int Xb)
{
       minimumX = Xa;
       maximumX = Xb;
       movingRight = true;
}
public void act()
{
     if(getX() < minimumX)
     {
        movingRight = true;
     } else if(getX > maximumX)
     {
        movingRight = false;
     }
     if(movingRight)
     { 
         setLocation(getY(), getX + 1);
     } else if(!movingRight)
     {
         setLocation(getY(), getX - 1);
     }
}
dark_sky dark_sky

2012/5/11

#
Great idea, thank you, erdelf! (I could have figured that out by myself :x. Thought it would be more complicated)
danpost danpost

2012/5/12

#
I would be even less complicated if the boolean 'movingRight' was replaced by an int 'direction'.
private int direction;
private int maximumX;
private int minimumX;

public void moving platform(int Xa, int Xb)
{
       minimumX = Xa;
       maximumX = Xb;
       direction = 1;
}
public void act()
{
     if(getX() <= minimumX) direction = 1;
     if(getX() >= maximumX) direction = -1;
     setLocation(getX() + direction, getY());
}
dark_sky dark_sky

2012/5/13

#
I still have a little question: How can I make my Player to stay on a moving platform so that he doesn't fall down when the platform moves? I implemented the standing by defining a method checkStanding() which sets vSpeed to 0 when canStand() (which checks for an intersecting platform at getImage().getHeight()/2) returns true. Thank you
danpost danpost

2012/5/13

#
You could add an instance variable to the Platform class to hold a reference to the player on the platform (or null, if none).
private Player playerOn = null;

public void setPlayerOn(Player player)
{
    playerOn = player;
}
Then, when you move the platform, check for 'player' not being 'null', and if not, also move the player the same amount. If the player moves or jumps off the platform, set the platform 'playerOn' variable back to 'null'. It might be wise to have an instance variable 'onPlatform' to hold a reference to the platform the player landed on. ** On the other hand ** If you only have one main Player character, you could put the 'onPlatform' variable in the world class, initially set to null (unless you start your player on one). Either way, when jumping or falling (or moving off onto another platform or a floor), make sure you reset it to 'null' (or the new platform). The reference to the player (if only one) does not have to be in a platform instance variable, as you probably have one in the world class. Let us say your world is called 'PlatformWorld'. Then, in the world class you have instance variables:
Player player = new Player();
Platform onPlatform = null;
and, in the Platform class (within the act method, or where you call setLocation on the platform):
setLocation(getX() + direction, getY()); // direction being specified
// check to see if player is on 'this' platform
if (this.equals(((PlatformWorld) getWorld()).onPlatform)
{ // if so, move the player
    Player playerOn = ((PlatformWorld) getWorld()).player;
    playerOn.setLocation(playerOn.getX() + direction, playerOn.getY());
}
Each time the player moves, regardless of the platform, reset the 'onPlatform' variable in the world class. I hope this makes sense (and helps).
You need to login to post a reply.