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?


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | 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 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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()); } |
1 2 3 4 5 6 | private Player playerOn = null ; public void setPlayerOn(Player player) { playerOn = player; } |
1 2 | Player player = new Player(); Platform onPlatform = null ; |
1 2 3 4 5 6 7 | 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()); } |