0

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:

        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);
        }

Within the tile class:

            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.

  • I think that what you're asking for is called parallax scrolling, google for some parallax scrolling tutorials? – ejbs Jan 18 '14 at 18:43
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve). – Andrew Thompson Jan 18 '14 at 21:34
  • Thank you for the replies, I will be using a parallax BG but I can do that part, also, sorry I thought it was an mcve. The part I am having problems with is the variable scope of how to use the setMovingLeft boolean from a different class – user3209948 Jan 19 '14 at 23:02

0 Answers0