Can't figure out how to use JFrame's add() method right
I'm trying to make a game with multiple levels, but once I load the second level I can't move my character on screen. I know where the problem is, but I don't fully know what's going on or how to fix the problem.
The problem is adding the classes "Inner" and "Inner2" to the JFrame. I can add the variable "inner" to the JFrame easily enough by putting it at the beginning of the constructor right after the call for super(); but if I put it anywhere else I am not able to move in my game.
Everything was working just fine until I needed more than one level, so I added the Swing timer to constantly check for when the player went over a certain limit, where the game was then supposed to remove the first level from the JFrame and then add the second level.
I tried this and I found myself not able to move in the game.
Code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class ProjectWindow extends JFrame implements ActionListener
{
public Inner inner;
public Inner2 inner2;
public boolean level1;
public Timer timer;
private int check = 0;
private boolean level1unf = true;
ProjectWindow()
{
super();
inner = new Inner();
add(inner); // If I put this anywhere else, I can't move in the game
setTitle("Civil War Game");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(600, 500);
setBounds(75,75,600,500);
setVisible(true);
level1 = true;
// if I were to put add(inner); here, it would not let me move in game.
timer = new Timer(100, this);
}
public void Level()
{
if(level1)
{
level1 = false;
timer.start();
}
}
public void actionPerformed(ActionEvent e)
{
check = inner.getInt();
if(check > 1500 && level1unf)
{
level1unf = false;
remove(inner);
inner2 = new Inner2();
add(inner2); // this is where the issue is. Because I can't add
//everything in the constructor, I have to add the second level here.
// The game will basically freeze and I won't be able to move.
}
}
}