Note that there are some explanatory texts on larger screens.

plurals
  1. POjava looping with case, while, and if statements
    primarykey
    data
    text
    <p>I've been working on a small project trying to learn some basics, I'm trying to make a clone of space invaders. I'm not really experienced (which is why I'm doing this) and I've run into something I've never had a problem with before.</p> <p>My problem is with loops, I've used basic loops but I'm using some nested loops now and it's giving me some problems. Here is the code that breaks my project</p> <pre><code> public void moveLevel(int l, ArrayList ms){ switch(l){ case 1:{ centerX = 60; centerY = 35; alienArray = ms; moveRight = true; while(moveRight == true){ x += 1; } } case 2:{ } } } </code></pre> <p>I can show more code if anybody thinks it would help, but basically, this block gets the level number (l) passed to it as well as an array list which holds about 15 'alien' objects. The line 'x+=1' is what moves the aliens (the location of each alien is x). This code is called from another function which is constantly called from a swing timer.</p> <p>What is happening, is that when the code reaches this point, the program seems to freeze. I have a button on the JPanel that doesn't react, I have a hotkey to close the application which doesn't react, and exiting the application with the mouse does nothing (I've included a DefaultCloseOperation(EXIT_ON_CLOSE) in the JFrame which works without this while loop).</p> <p>if I replace the word 'while' with 'if', like below the code works fine.</p> <pre><code> public void moveLevel(int l, ArrayList ms){ switch(l){ case 1:{ centerX = 60; centerY = 35; alienArray = ms; moveRight = true; if(moveRight == true){ x += 1; } } case 2:{ } } } </code></pre> <p>I've also tried a do, while loop. </p> <p>I have no idea what the problem is, I assume it's a logic error but it seems fairly straightforward to me. Again, if anybody would like to see the rest of the code I can post it. Otherwise, if anybody has any suggestions I would appreciate it. I'm open to specific advice or just general advice on code efficiency. Thanks</p> <h1>ANSWER</h1> <p>Okay I've gotten my code moving forward thanks to Ted Hopp, who commented below. Looks like an infinite loop was being executed within the case statement.</p> <p>Here is my fix for any who are curious, I've included the function that calls the function from the original post.</p> <pre><code> public void move(int l, ArrayList ms){ level = l; alienArray = ms; moveLevel(level, alienArray); centerX += horizontal; centerY += vertical; x += horizontal; y += vertical; if(moveRight == true){ horizontal = 1; vertical = 0; System.out.println(centerX); } else x -= 1; } public void moveLevel(int l, ArrayList ms){ switch(l){ case 1:{ alienArray = ms; moveRight = true; if(moveRight == true){ if (centerX &gt; 300){ moveRight = false; } if(moveRight == false){ if(centerX &lt; 100){ } } } } break; case 2:{ } } } </code></pre> <p>This just basically moves all of the aliens to the right and stops so far, but I've moved past the original issue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload