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 5 of 5

Thread: Why do I get an infinite loop?

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

    Question Why do I get an infinite loop?

    I am trying to make a ball game that will simply make some bricks and such and destroy them when they get hit by the ball. The ball, paddle, brick, and such are all using their own classes so I don't have a huge mess but my code is pretty lengthy but I can post it here if need be. I have this bit of code that I'm not sure why it does this. I have the ball starting off inside the window at x = 100 and y = 400 and the window is about 600 by 600 I think but the point of the matter is, the ball starts off in the window. I wanted to make it to where if you hit the bottom, the game stops, but I can't get it to stop incorrectly issuing out the you lose code. I notice in my netbeans, it starts up, then it says you lose three times before the game even starts. I don't understand why. Can someone here please explain?

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package bouncing.ball;
     
    /**
     *
     * @author Marlin
     */
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.LinkedList;
     
    public class BouncePanel extends JPanel implements KeyListener {
     
        //location of bouncing ball
        Ball ball;
        Paddle paddle;
        LinkedList<Brick> brickList;
        private int x;
        private int y;
        private int dx = 5;
        private int dy = 5;
        int i = 0;
     
        BouncePanel() {
            ball = new Ball();
            paddle = new Paddle();
            brickList = new LinkedList<Brick>();
     
            makeABunchOfBricks();
     
            x = 100;
            y = 50;
     
            AnimationThread animethread = new AnimationThread(this);
            animethread.start();
            //keyboard stuff
            addKeyListener(this);
            setFocusable(true);
        }
     
        public void paintComponent(Graphics g) {
            //clear screen
            super.paintComponent(g);
     
            //draw the ball
            ball.draw(g);
     
            //draw the paddle
            paddle.draw(g);
     
            //draw the bricks
            for (Brick b : brickList) {
                b.draw(g);
            }
     
        }
     
        public void update() throws InterruptedException {
     
            ball.update();
            paddle.update();
     
            //check for wall collisions
            if (ball.x <= 0 || ball.x + ball.getWidth() >= getWidth()) {
                ball.dx = -1 * ball.dx;
            }
            if (ball.y <= 0) {
                ball.dy = -1 * ball.dy;
            }
            if (ball.y + ball.getHeight() >= getHeight())
            {
                System.out.println("You Lose!!!");
            }
     
            //check for paddle collision
            if (ball.intersects(paddle)) {
                ball.dy = -1 * ball.dy;
                if (paddle.dx == 10) {
                    ball.dx = 8;
                }
                if (paddle.dx == -10) {
                    ball.dx = -8;
                }
                if (paddle.dx == 0 && ball.dx == 8) {
                    ball.dx = 4;
                }
                if (paddle.dx == 0 && ball.dx == -8) {
                    ball.dx = -4;
                }
            }
     
            //check for brick collisions
            for (Brick b : brickList) {
                if (ball.intersects(b) && !b.destroyed) {
                    b.destroy();
                    ball.dy = -1 * ball.dy;
                }
            }
     
            repaint();
        }
     
        public void makeABunchOfBricks() {
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    brickList.add(new Brick(i * 60, j * 30, 50, 20));
                }
            }
        }
     
        //key listener methods
        public void keyPressed(KeyEvent e) {
            if (e.getKeyChar() == 'd') {
                paddle.dx = 10;
            }
            if (e.getKeyChar() == 'a') {
                paddle.dx = -10;
            }
        }
     
        public void keyReleased(KeyEvent e) {
            paddle.dx = 0;
            paddle.dy = 0;
        }
     
        public void keyTyped(KeyEvent e) {
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why do I get an infinite loop?

    How can the code be tested? I don't see a main() method to start the execution.

    Try debugging the code by adding calls to the println() method. Print out the values of all the variables that control the program's execution as they are changed and used so that you can see what the computer sees which will help you understand why the code is doing what it is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why do I get an infinite loop?

    Hmm....I could try that but not until later today but like I mentioned, for some reason when I added a println to the method in question, it sometimes goes infinite or it will output the code three times in a row even thought, from what I can see, the conditions are not being met.

    Here is the main method.
    I didn't include it here cause I didn't know if it was needed.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package bouncing.ball;
     
    /**
     *
     * @author Marlin
     */
    import javax.swing.*;
     
    public class Driver {
     
        public static void main(String[] args) {
            //create a frame
            JFrame frame = new JFrame("Fun bouncing ball");
     
            //create a panel, put in frame
            BouncePanel panel = new BouncePanel();
            frame.getContentPane().add(panel);
     
            //set frame size, make visible
            frame.setSize(600, 600);
            frame.setVisible(true);
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why do I get an infinite loop?

    when I added a println to the method in question, it sometimes goes infinite or it will output the code three times in a row even thought, from what I can see, the conditions are not being met.
    Please post the debug output that shows what is being traced and how the values changed as the coded executed.

    The posted code doesn't have any printlns in it for debugging. How are you debugging the code?


    The posted code does not compile because of missing class definitions. It can not be executed for testing unless it compiles.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why do I get an infinite loop?

    Ok so I did some println on the code but the reason it gives you that error is cause I didn't include the classes since it's somewhat numerous. I notice that when I start the program, for some reason, the getHeight takes a few cycles to initialize. For like three or four cycles, getHeight is at 0 then it sets itself 600 like it is suppose to or to whatever height i set to it initialize.

Similar Threads

  1. Infinite loop?
    By jackfletcher in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2012, 01:06 PM
  2. [SOLVED] Infinite loop issue
    By Sharmeen in forum Loops & Control Statements
    Replies: 1
    Last Post: October 20th, 2012, 12:18 PM
  3. Infinite loop problem
    By jacobjc3 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 30th, 2012, 09:41 PM
  4. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM