Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Question Movement/KeyListener freezing..

    Hi.

    I'm making a game which have a single frame but the contents in the frame oftenly change, such as when you change a different level of the game. I'm using tons of booleans to do this but i've got a problem.. When you press the button going back to main menu, it sets startscreen = true, lvl1 = false and so on. But when i do this, movement freezes/KeyListener freeze, cause i've done so that you can move around with the character at the main menu. This freezing occurs when you press back to main menu or back(from level select screen/options screen), and the freezing goes away when you refresh the screen(press a new button/starting a level). I can't find anything in the code that may causing this, but it might be that some code is wrongly placed, but since i don't know where the problem is, i can't really post the code, cause it's too much to post.

    And a odd thing is, when i finish a level, and it goes back to main menu, the keylistener dosen't freeze, so i placed that lvl finish code inside the "back" button body. This worked, if i removed Controlscreen = false or Levelscreen = false. So i'd guess the booleans are the bad guys in this..

    This almost works, it dosen't remove the stuff that's on the options/levelscreen cause if i turn controlscreen/levelscreen false, the movement freezes.. NOTE: The program dosen't freeze, only keylistening.
    PHP Code:
    if(e.getSource() == Back){
    	//controlscreen = false; levelscreen = false;
    	lvl1done = true;
    	playerspeedy = 0;
    	playerspeedx = 0;
    	startscreen = true;
    	player = new Starter(20, 330);
    	lvl1 = false;
    }
    Last edited by AndroidAppNewbie; 03-10-2011 at 03:17 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Sorry, but if you want help with this, you're going to have to post an SSCCE. That does not mean you should post your entire program- it means that you should boil your problem down into as few lines as possible.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    I tried to make it as short as possible, hopefully you can see something wrong :/

    PHP Code:
    public class Cube extends JPanel implements ActionListener,KeyListener{
    
    Starter player;	
    
    public Cube() throws Exception{
    		setLayout(null);
    	
    		player = new Starter(20, 330);
    
    LevelSelect = new JButton("Level Select");
    LevelSelect.addActionListener(this);
    LevelSelect.setBounds(310, 240, 300 ,60);
    add(LevelSelect);
    
    Howto = new JButton("Shapes & Controls");
    Howto.addActionListener(this);
    Howto.setBounds(310, 320, 300 ,60);
    add(Howto);
    
    Exit = new JButton("Exit");
    Exit.addActionListener(this);
    Exit.setBounds(310, 400, 300 ,60);
    add(Exit);
    
    Back = new JButton("Back to Main Menu");
    Back.addActionListener(this);
    Back.setBounds(10, 50, 150 ,40);
    add(Back);
    Exit2 = new JButton("Exit");
    Exit2.addActionListener(this);
    Exit2.setBounds(10, 100, 150 ,40);
    add(Exit2);
    
                    setVisible(true);
    		setSize(900, 500);
    		setBackground(Color.BLACK);
    		setLocation(500, 250);
    		addKeyListener(this);
    		setFocusable(true);
    
    }
    public void keyPressed(KeyEvent e) {
    //MOVEMENT     
    		if(e.getKeyCode() == KeyEvent.VK_W){
    			playerspeedy = -1;
    		}if(e.getKeyCode() == KeyEvent.VK_S){
    			playerspeedy = 1;
    		}if(e.getKeyCode() == KeyEvent.VK_A){
    			playerspeedx = -1;
    		}if(e.getKeyCode() == KeyEvent.VK_D){
    			playerspeedx = 1;
    		}
    
    public void actionPerformed(ActionEvent e) {
    
    if(startscreen){
    			LevelSelect.setVisible(true);
    			Howto.setVisible(true);
    			Exit.setVisible(true);
    			
    			Back.setVisible(false);
    			Exit2.setVisible(false);
    			
    
    		}if(lvl1||lvl2||lvl3){
    			add(GameBack);
    			add(GameReset);
    		}else{
    			remove(GameBack);
    			remove(GameReset);
    		}
    	//////////////////////////////////////////////////// LEVEL SELECT
    	if(!levelscreen){
    		remove(level1);
    		remove(level2);
    		remove(level3);
    		
    		Back.setVisible(false);
    		Exit2.setVisible(false);
    	}if(levelscreen){
    		Howto.setVisible(false);
    		LevelSelect.setVisible(false);
    		Exit.setVisible(false);
    		add(level1);
    		add(level2);
    		add(level3);
    		
    		Back.setVisible(true);
    		Exit2.setVisible(true);
    	}if(controlscreen){
    		Howto.setVisible(false);
    		LevelSelect.setVisible(false);
    		Exit.setVisible(false);
    		
    		Back.setVisible(true);
    		Exit2.setVisible(true);
    	}
    PHP Code:
    public static void main(String[]args) throws Exception{
    	new Starter();
    }
    	
    int x,y;
    
    	public Starter() throws Exception{
    		super("The Cube");
    		Cube cu = new Cube();
    		addKeyListener(cu);
    		add(cu);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		setLocation(500, 250);
    		setResizable(false);
    		setSize(900, 500);
    	}
    public Starter(int startX, int startY){
    		x = startX;
    		y = startY;
    	}
    }

  4. #4
    Join Date
    Sep 2010
    Posts
    47
    Rep Power
    0

    Default

    I made a fix which works good, i don't know if it'll stay good but hopefully it will. I made timers, so it's a slight delay between press and control/levelscreen turning false..the movement didn't freeze now.. so maybe it was some sort of overload when they ran at the same time..i don't know..

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    Sorry, but that code isn't really helpful- it contains references to classes that are missing, etc. I'd be willing to bet that your problem is something simple, but I can't really say for sure without seeing an SSCCE. Up to you.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Program Freezing While Reading Input
    By airowe in forum New To Java
    Replies: 15
    Last Post: 01-11-2011, 03:38 AM
  2. why does this code keep freezing?
    By berkeley in forum New To Java
    Replies: 4
    Last Post: 06-03-2010, 04:41 PM
  3. Replies: 2
    Last Post: 03-16-2009, 02:33 PM
  4. Movement of balls
    By BlitzA in forum New To Java
    Replies: 8
    Last Post: 01-09-2008, 03:30 PM
  5. freezing when use socket.accept() inside of a thread
    By tamayo in forum Advanced Java
    Replies: 0
    Last Post: 07-23-2007, 11:48 PM

Posting Permissions

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