Results 1 to 2 of 2
  1. #1
    OurOhnlyHope is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Assistance on transforming a KeyListener to a KeyBinding?

    These are excerpts from the source code I am working with:

    Java Code:
    	public Pokemon(){
    		currentBGM = title;
    		currentBGM.start();
    		menu = new MenuScene(this);
    		col.loadClip("Audio/SE/Collision.wav", "Collision", 1);
    	    col.loadClip("Audio/SE/Select.wav", "Select", 1);
    	    col.loadClip("Audio/SE/Menu.wav", "Menu", 1);
    	    col.loadClip("Audio/SE/Damage.wav", "Damage", 1);
    		setBackground(Color.BLACK);
    	    setPreferredSize(new Dimension(480,320));
    		
                    addKeyListener(this);
    
    		gameTimer = new javax.swing.Timer(350, this);
    	    gameTimer.start();
    	}
    Java Code:
    @Override
         public void keyPressed(KeyEvent e) {
         	setFocusable(true);
            int keyCode = e.getKeyCode();
         	if (atTitle == true) {
         		if (keyCode == KeyEvent.VK_ENTER) {
         			gameTimer.setDelay(1);
    	        	atTitle = false;
    	        	currentBGM.stop();
    				currentBGM = continuebgm;
    				currentBGM.start();
    	        	atContinueScreen = true;
    	        }
         	}
    The Key Listener in this is faulty and I know that it's the reason my program isn't working correctly; when at the title screen of the program, the enter key is almost never recognized and as such, there's no way for the player to continue to the game. I also know that I need to replace the Key Listener with a Key Binding. The problem is that I absolutely stink at following Java tutorials and I don't know where to start. I've tried futzing around with various ways of writing the code like some of the tutorials I've looked at suggested, but I only exacerbated my own problem.

    Can anybody either walk me through this transition or tell me how I should implement a Key Binding in this situation?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Assistance on transforming a KeyListener to a KeyBinding?

    Moved from New to Java.

    The Oracle tutorial on Key Bindings *does* provide a step-by-step. Which part of it are you stuck on?

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

Similar Threads

  1. KeyBinding not responsive
    By Germoose in forum New To Java
    Replies: 3
    Last Post: 04-21-2012, 11:10 PM
  2. Keybinding
    By mine0926 in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 05:33 AM
  3. KeyBinding Help
    By Lil_Aziz1 in forum New To Java
    Replies: 12
    Last Post: 07-27-2010, 03:58 PM
  4. Transforming XML to HTML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-01-2008, 10:06 PM
  5. transforming double to int
    By AlejandroPenton in forum New To Java
    Replies: 2
    Last Post: 12-11-2007, 01:34 AM

Posting Permissions

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