I have been following a 2d side scroller tutorial in java and have got the basics working, I changed the code a bit to make it so that the tiles and background are always moving, this worked as i intended. The relevant code is below:
Within the character class:
Within the tile class:if (speedX < 0) { centerX += speedX; } if (speedX == 0 || speedX < 0) { bg1.setSpeedX(0); bg2.setSpeedX(0); } if (centerX <= 350 && speedX > 0) { centerX = centerX + speedX ; } if (speedX > 0 && centerX > 350) { bg1.setSpeedX(-MOVESPEED / 4); bg2.setSpeedX(-MOVESPEED / 4); }
speedX = bg.getSpeedX() * 3; tileX = tileX + speedX - 4;
I wanted a way to make it so that if the character moves left, the tiles + background move slower. I am having issues with variable scope and setters/getters. Within the main game class, I have the code:
case KeyEvent.VK_LEFT: character.moveLeft(); character.setMovingLeft(true); break;
How can i include an if statement within the tile class that reads whether the character is moving left (determined from a keyPressed event in the main game class) and adjusts the speed of the tiles accordingly?
I want to write something to the effect of:
if (setMovingLeft = true){ speedX = bg.getSpeedX() * 3; tileX = tileX + speedX - 1; << changed from - 4
Obviously this doesn't work, can anyone suggest a way to accomplish this? I can provide more information if needed.