Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: 2d sidescroller moving background in java

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2d sidescroller moving background in java

    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.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: 2d sidescroller moving background in java

    '==' is the operator to check for equality. Further, comparing a boolean to 'true' or 'false' in a conditional statement is unnecessary. Simply if( flag ) or if ( !flag ) where 'flag' is a boolean variable is sufficient.

Similar Threads

  1. Creating a background service in java
    By prashant.6388 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2013, 11:00 PM
  2. Asynchronous background processing in JAVA.
    By Asir Jefferson in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 21st, 2013, 06:45 AM
  3. background image in java form
    By Pearly in forum AWT / Java Swing
    Replies: 2
    Last Post: January 20th, 2013, 09:11 AM
  4. background image in java form
    By Pearly in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 20th, 2013, 09:11 AM
  5. Java Program that Runs in Background
    By 1bun100 in forum Java Theory & Questions
    Replies: 7
    Last Post: September 15th, 2011, 02:10 PM

Tags for this Thread