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?


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); } }
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()); }
private Player playerOn = null; public void setPlayerOn(Player player) { playerOn = player; }
Player player = new Player(); Platform onPlatform = null;
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()); }