Results 1 to 4 of 4
  1. #1
    GamerzX is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default 2-Directional Scrolling Background in a 2D Game - it doesnt work, please help

    I am acreating a 2D game and I need oyur help guys!

    I have encountered a problem when I wanted to update the code, which will allow my character to go to opposite direction.

    The background was scrolling only in right direction, and the robot could not go left and the background was not scrolling in the opposite direction (left).
    So I decided to fix that and added this code (see below) to the Robot class in the public void update () section (Now my character can go back to the opposite direction with scrolling background).
    But my problem is that I want my character to stop moving to left and the background should stop scrolling left when im at characters spawn point (at the beginning - because left of that, there is no more background, see picture bellow)

    This is the code I updated:

    Java Code:
     if (speedX < 0 && centerX < 100) { 
          bg1.setSpeedX(MOVESPEED); 
          bg2.setSpeedX(MOVESPEED); 
          centerX = 100;
    I will copy 3 Classes that are important to us (Starting Class, Robot, Background)

    #1 Starting Class

    Java Code:
    package kiloboltgame;
    
        import java.applet.Applet;
        import java.awt.Color;
        import java.awt.Frame;
        import java.awt.Graphics;
        import java.awt.Image;
        import java.awt.event.KeyEvent;
        import java.awt.event.KeyListener;
        import java.net.URL;
        import java.util.ArrayList;
        import kiloboltgame.framework.Animation;
    
    public class StartingClass extends Applet implements Runnable, KeyListener {
    
    private Robot robot;
    private Heliboy hb, hb2;
    private Image image, currentSprite, character, character2, character3,
            characterDown, characterJumped, background, heliboy, heliboy2,
            heliboy3, heliboy4, heliboy5;
    private Graphics second;
    private URL base;
    private static Background bg1, bg2;
    private Animation anim, hanim;
    
    @Override
    public void init() {
    
        setSize(800, 480);
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Q-Bot Alpha");
        try {
            base = getDocumentBase();
        } catch (Exception e) {
            // TODO: handle exception
        }
    
        // Image Setups
    
        character = getImage(base, "data/character.png");
        character2 = getImage(base, "data/character2.png");
        character3 = getImage(base, "data/character3.png");
    
        characterDown = getImage(base, "data/down.png");
        characterJumped = getImage(base, "data/jumped.png");
    
        heliboy = getImage(base, "data/heliboy.png");
        heliboy2 = getImage(base, "data/heliboy2.png");
        heliboy3 = getImage(base, "data/heliboy3.png");
        heliboy4 = getImage(base, "data/heliboy4.png");
        heliboy5 = getImage(base, "data/heliboy5.png");
    
        background = getImage(base, "data/background.png");
    
        anim = new Animation();
        anim.addFrame(character, 1250);
        anim.addFrame(character2, 50);
        anim.addFrame(character3, 50);
        anim.addFrame(character2, 50);
    
        hanim = new Animation();
        hanim.addFrame(heliboy, 100);
        hanim.addFrame(heliboy2, 100);
        hanim.addFrame(heliboy3, 100);
        hanim.addFrame(heliboy4, 100);
        hanim.addFrame(heliboy5, 100);
        hanim.addFrame(heliboy4, 100);
        hanim.addFrame(heliboy3, 100);
        hanim.addFrame(heliboy2, 100);
    
        currentSprite = anim.getImage();
    
    }
    
    @Override
    public void start() {
        bg1 = new Background(0, 0);
        bg2 = new Background(2160, 0);
        hb = new Heliboy(340, 360);
        hb2 = new Heliboy(700, 360);
        robot = new Robot();
    
        Thread thread = new Thread(this);
        thread.start();
    }
    
    @Override
    public void stop() {
        // TODO Auto-generated method stub
    }
    
    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }
    
    @Override
    public void run() {
        while (true) {
            robot.update();
            if (robot.isJumped()) {
                currentSprite = characterJumped;
            } else if (robot.isJumped() == false && robot.isDucked() == false) {
                currentSprite = anim.getImage();
            }
    
            ArrayList projectiles = robot.getProjectiles();
            for (int i = 0; i < projectiles.size(); i++) {
                Projectile p = (Projectile) projectiles.get(i);
                if (p.isVisible() == true) {
                    p.update();
                } else {
                    projectiles.remove(i);
                }
            }
    
            hb.update();
            hb2.update();
            bg1.update();
            bg2.update();
    
            animate();
            repaint();
    
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    public void animate() {
        anim.update(10);
        hanim.update(200);
    }
    
    @Override
    public void update(Graphics g) {
        if (image == null) {
            image = createImage(this.getWidth(), this.getHeight());
            second = image.getGraphics();
        }
    
        second.setColor(getBackground());
        second.fillRect(0, 0, getWidth(), getHeight());
        second.setColor(getForeground());
        paint(second);
    
        g.drawImage(image, 0, 0, this);
    
    }
    
    @Override
    public void paint(Graphics g) {
        g.drawImage(background, bg1.getBgX(), bg1.getBgY(), this);
        g.drawImage(background, bg2.getBgX(), bg2.getBgY(), this);
    
        ArrayList projectiles = robot.getProjectiles();
        for (int i = 0; i < projectiles.size(); i++) {
            Projectile p = (Projectile) projectiles.get(i);
            g.setColor(Color.YELLOW);
            g.fillRect(p.getX(), p.getY(), 10, 5);
        }
    
        g.drawImage(currentSprite, robot.getCenterX() - 61,
                robot.getCenterY() - 63, this);
        g.drawImage(hanim.getImage(), hb.getCenterX() - 48, hb.getCenterY() - 48, this);
        g.drawImage(hanim.getImage(), hb2.getCenterX() - 48, hb2.getCenterY() - 48, this);
    }
    
    @Override
    public void keyPressed(KeyEvent e) {
    
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            System.out.println("Move up");
            break;
    
        case KeyEvent.VK_DOWN:
            currentSprite = characterDown;
            if (robot.isJumped() == false) {
                robot.setDucked(true);
                robot.setSpeedX(0);
            }
            break;
    
        case KeyEvent.VK_LEFT:
            robot.moveLeft();
            robot.setMovingLeft(true);
            break;
    
        case KeyEvent.VK_RIGHT:
            robot.moveRight();
            robot.setMovingRight(true);
            break;
    
        case KeyEvent.VK_SPACE:
            robot.jump();
            break;
    
        case KeyEvent.VK_CONTROL:
            if (robot.isDucked() == false && robot.isJumped() == false) {
                robot.shoot();
            }
            break;
    
        }
    
    }
    
    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            System.out.println("Stop moving up");
            break;
    
        case KeyEvent.VK_DOWN:
            currentSprite = anim.getImage();;
            robot.setDucked(false);
            break;
    
        case KeyEvent.VK_LEFT:
            robot.stopLeft();
            break;
    
        case KeyEvent.VK_RIGHT:
            robot.stopRight();
            break;
    
        case KeyEvent.VK_SPACE:
            break;
    
        }
    
    }
    
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    
    }
    
    public static Background getBg1() {
        return bg1;
    }
    
    public static Background getBg2() {
        return bg2;
    }
    #2 Robot

    Java Code:
     package kiloboltgame;
    
         import java.util.ArrayList;
    
         public class Robot {
    
    // Constants are Here
    final int JUMPSPEED = -15;
    final int MOVESPEED = 5;
    final int GROUND = 382;
    
    private int centerX = 50;
    private int centerY = GROUND;
    private boolean jumped = false;
    private boolean movingLeft = false;
    private boolean movingRight = false;
    private boolean ducked = false;
    
    private int speedX = 0;
    private int speedY = 1;
    
    private Background bg1 = StartingClass.getBg1();
    private Background bg2 = StartingClass.getBg2();
    
    private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
    
    public void update() {
        // Moves Character or Scrolls Background accordingly.
    
        if (speedX < 0) {
            centerX += speedX;
    
        }
    
        if (speedX == 0 || speedX < 0) {
            bg1.setSpeedX(0);
            bg2.setSpeedX(0);
        }
    
        if (centerX <= 400 && speedX > 0) {
            centerX += speedX;
        }
    
        if (speedX > 0 && centerX > 400) {
            bg1.setSpeedX(-MOVESPEED);
            bg2.setSpeedX(-MOVESPEED);
        }
    
        if (speedX < 0 && centerX < 100) {
            bg1.setSpeedX(MOVESPEED);
            bg2.setSpeedX(MOVESPEED);
            centerX = 100;
    
    
    
        }
    
    
    
        // Updates Y Position
        centerY += speedY;
        if (centerY + speedY >= GROUND) {
            centerY = GROUND;
        }
    
        // Handles Jumping
        if (jumped == true) {
            speedY += 1;
    
            if (centerY + speedY >= GROUND) {
                centerY = GROUND;
                speedY = 0;
                jumped = false;
            }
        }
    
    }
    
    public void moveRight() {
        if (ducked == false) {
            speedX = MOVESPEED;
        }
    }
    
    public void moveLeft() {
        if (ducked == false) {
            speedX = -MOVESPEED;
    
        }
    
    }
    
    public void stopRight() {
        setMovingRight(false);
        stop();
    }
    
    public void stopLeft() {
        setMovingLeft(false);
        stop();
    
    }
    
    private void stop() {
        if (isMovingRight() == false && isMovingLeft() == false) {
            speedX = 0;
        }
    
        if (isMovingRight() == false && isMovingLeft() == true) {
            moveLeft();
        }
    
        if (isMovingRight() == true && isMovingLeft() == false) {
            moveRight();
        }
    
    }
    
    public void jump() {
        if (jumped == false) {
            speedY = JUMPSPEED;
            jumped = true;
        }
    
    }
    
    public void shoot() {
        Projectile p = new Projectile(centerX + 50, centerY - 25);
        projectiles.add(p);
    }
    
    public int getCenterX() {
        return centerX;
    }
    
    public int getCenterY() {
        return centerY;
    }
    
    public boolean isJumped() {
        return jumped;
    }
    
    public int getSpeedX() {
        return speedX;
    }
    
    public int getSpeedY() {
        return speedY;
    }
    
    public void setCenterX(int centerX) {
        this.centerX = centerX;
    }
    
    public void setCenterY(int centerY) {
        this.centerY = centerY;
    }
    
    public void setJumped(boolean jumped) {
        this.jumped = jumped;
    }
    
    public void setSpeedX(int speedX) {
        this.speedX = speedX;
    }
    
    public void setSpeedY(int speedY) {
        this.speedY = speedY;
    }
    
    public boolean isDucked() {
        return ducked;
    }
    
    public void setDucked(boolean ducked) {
        this.ducked = ducked;
    }
    
    public boolean isMovingRight() {
        return movingRight;
    }
    
    public void setMovingRight(boolean movingRight) {
        this.movingRight = movingRight;
    }
    
    public boolean isMovingLeft() {
        return movingLeft;
    }
    
    public void setMovingLeft(boolean movingLeft) {
        this.movingLeft = movingLeft;
    }
    
    public ArrayList getProjectiles() {
        return projectiles;
    }
    
           }
    #3 Background

    Java Code:
    package kiloboltgame;
    
        public class Background {
    private int bgX, bgY, speedX;
    
    public Background(int x, int y) {
        bgX = x;
        bgY = y;
        speedX = 0;
    }
    
    public void update() {
        bgX += speedX;
    
        if (bgX <= -2160) {
            bgX += 4320;
        }
    
    }
    
    public int getBgX() {
        return bgX;
    }
    
    public int getBgY() {
        return bgY;
    }
    
    public int getSpeedX() {
        return speedX;
    }
    
    public void setBgX(int bgX) {
        this.bgX = bgX;
    }
    
    public void setBgY(int bgY) {
        this.bgY = bgY;
    }
    
    public void setSpeedX(int speedX) {
        this.speedX = speedX;
    }
    Picture:

    2-Directional Scrolling Background in a 2D Game - it doesnt work, please help-23.png

    Thank you in advance!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: 2-Directional Scrolling Background in a 2D Game - it doesnt work, please help

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    GamerzX is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: 2-Directional Scrolling Background in a 2D Game - it doesnt work, please help

    Quote Originally Posted by DarrylBurke View Post
    Yes I posted it there, what does this has to do with that forum?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,728
    Rep Power
    5

    Default Re: 2-Directional Scrolling Background in a 2D Game - it doesnt work, please help

    Well imagine this: you posted a huge chunk of code. Someone takes an interest and goes to dig through your code trying to understand what is going on and to try and find the answer to your questions. This person invests a great deal of time doing that and also to formulate an answer that you will be able to understand.

    Only to figure out that all that time and effort was wasted because you already go an answer in the crosspost you neglected to mention. Imagine that happens to you, wouldn't you be immensely pissed at the person who cross posted? All you had to do is come clean that you did it by posting a link so that people may go to the other post and see if you were already answered.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Constant scrolling background
    By mwr1976 in forum Java 2D
    Replies: 5
    Last Post: 04-13-2012, 07:32 AM
  2. relation doesnt work
    By Nigel in forum New To Java
    Replies: 3
    Last Post: 03-12-2011, 07:02 PM
  3. PrintWriter doesnt work :(
    By Addez in forum New To Java
    Replies: 11
    Last Post: 01-17-2010, 05:59 PM
  4. JFrame window doesnt change background color
    By Addez in forum New To Java
    Replies: 7
    Last Post: 11-07-2009, 09:38 PM
  5. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 09:31 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •