Results 1 to 2 of 2
- 01-20-2011, 11:11 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 7
- Rep Power
- 0
Problem with Keylistener, some help pls
Heya, would apreciate some help if possible.. to start with, im sry that i didnt display my code the right way (couldnt find the option/ thread about it), anyway... my problem is this:
My school work is to make the Bomberman game, and i have 3 classes extending JPanel (menu, singleplayer and high scores), each of them take as parameter the same JFrame "myGame" wich i creat in the main class.
My idea is that each time i start one of those 3 classes.. the JFrame will have as contentPane the corresponding JPanel.
If i start with the SinglePlayerPanel, my keylistener is working fine and i can move my character.
if im inside a SinglePlayerPanel and i restart it (make a new one) it still works fine.
the problem comes if i change from MenuPanel into SinglePlayerPanel, the display changes and basicly everything works besides the Keylistener. (with everything else i mean TimerListeners, my "enemies" move fine aswell, etc).
My code for both MenuPanel and SinglePlayerPanel is this:
MenuPanel:
still have to complete the other actionEvents.Java Code:public class MenuFrame extends JPanel implements ActionListener { private Container contentPane; private JFrame myGame; private Image img = new ImageIcon("bomberman/GameFrames/img/bomberman Menu.png").getImage(); public MenuFrame(JFrame myGame) { this.myGame = myGame; JButton SinglePlayer = new JButton("Start"); JButton MultiPlayer = new JButton("Multiplayer"); JButton Load = new JButton("Load"); JButton HighScore = new JButton("High Scores"); JButton exit = new JButton("Exit"); SinglePlayer.setLocation(63, 443); MultiPlayer.setLocation(186, 443); Load.setLocation(340, 443); HighScore.setLocation(464, 443); exit.setLocation(629, 443); SinglePlayer.addActionListener(this); MultiPlayer.addActionListener(this); Load.addActionListener(this); HighScore.addActionListener(this); exit.addActionListener(this); add(SinglePlayer); add(MultiPlayer); add(Load); add(HighScore); add(exit); //myGame.setContentPane(contentPane); myGame.setContentPane(this); myGame.validate(); myGame.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Start")) { SinglePlayerFrame gui = new SinglePlayerFrame(myGame, 1, false); } else if (e.getActionCommand().equals("Multiplayer")) { } else if (e.getActionCommand().equals("Load")) { } else if (e.getActionCommand().equals("High Scores")) { } else if (e.getActionCommand().equals("Exit")) { } else System.out.println("Error in MenuFrame Listeners"); } public void paintComponent(Graphics g) { g.drawImage(img, 3, 8, null); super.paintComponents(g); } }
my SinglePlayerPanel:
Java Code:public class SinglePlayerFrame extends JPanel implements KeyListener { private GameTimers gameTimer; private JFrame myGame; public static BackgroundPanel backgroundIngameAlive = new BackgroundPanel(new ImageIcon("bomberman/GameFrames/img/Bomberman Fight.png").getImage()); public static BackgroundPanel backgroundIngameDead = new BackgroundPanel(new ImageIcon("bomberman/GameFrames/img/Bomberman Dead.png").getImage()); public static BackgroundPanel backgroundIngameDoorAvaible = new BackgroundPanel(new ImageIcon("bomberman/GameFrames/img/Bomberman Victorius.png").getImage()); public SinglePlayerFrame(JFrame myGame, int level, boolean continuation) { this.myGame = myGame; this.setLayout(new BorderLayout()); JPanel game = new JPanel(); JPanel rest = new JPanel(); //contentPane = myGame.getContentPane(); add(game, BorderLayout.CENTER); add(rest, BorderLayout.EAST); rest.setLayout(new BorderLayout()); JPanel bomberman = new JPanel(); JPanel information = new JPanel(); rest.add(information, BorderLayout.NORTH); rest.add(bomberman, BorderLayout.SOUTH); rest.add(backgroundIngameAlive, BorderLayout.CENTER); backgroundIngameAlive.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("bomber"), null)); information.setLayout(new GridLayout(3, 1)); information.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("information"), BorderFactory.createEmptyBorder(5, 5, 5 ,5))); information.add(time); information.add(points); information.add(enemysLeft); bomberman.setLayout(new GridLayout(6, 1)); bomberman.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Bomberman"), BorderFactory.createEmptyBorder(5, 5, 5 ,5))); bomberman.add(remoteControl); bomberman.add(bombs); bomberman.add(flameLevel); bomberman.add(speed); bomberman.add(totalLifes); bomberman.add(lifesLeftPowerUp); GameLevel gl = new GameLevel(Integer.toString(level), false, true, continuation); gameTimer = new GameTimers(gl, this); add(gameTimer, BorderLayout.CENTER); gameTimer.addKeyListener(this); gameTimer.setFocusable(true); gameTimer.requestFocusInWindow(); rest.setFocusable(false); myGame.setContentPane(this); myGame.validate(); myGame.setVisible(true); } public JFrame getMyGame() { return myGame; } public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if (GameLevel.bomber.isDead() == false) { if (keyCode == KeyEvent.VK_UP) { GameLevel.bomber.setCurrentCommand("up"); } else if(keyCode == KeyEvent.VK_DOWN) { GameLevel.bomber.setCurrentCommand("down"); } else if(keyCode == KeyEvent.VK_LEFT) { GameLevel.bomber.setCurrentCommand("left"); } else if(keyCode == KeyEvent.VK_RIGHT) { GameLevel.bomber.setCurrentCommand("right"); } else if(keyCode == KeyEvent.VK_SPACE) { gameTimer.placeBomb(GameLevel.bomber); } else if(keyCode == KeyEvent.VK_CONTROL) { } } else e.consume(); } public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); if ((keyCode == KeyEvent.VK_UP) || (keyCode == KeyEvent.VK_DOWN) || (keyCode == KeyEvent.VK_LEFT) || (keyCode == KeyEvent.VK_RIGHT)) { GameLevel.bomber.setCurrentCommand("idle"); } else e.consume(); } public void keyTyped(KeyEvent e) { e.consume(); } }
those backgroundPanels are a class i made so i could changes images depending on some situation in the game.
if i just run SinglePlayer i dont need gameTimer.requestFocusInWindow();, its there coz i was trying to find some solution.
well, since i already made a post about this... might aswell one other doubt i have xD, wich is: any idea why the buttons in MenuPanel arent changing to the location i wrote? (the total size of myGame is : 730 x 580.
thx in advance, sry for long post and probably confusing english (not my main language)
cumps
Moderator Edit: Code tags addedLast edited by Fubarable; 01-21-2011 at 01:28 AM. Reason: Moderator Edit: Code tags added
-
You likely have a focus issue which is not uncommon in this situation. I suggest you change from using KeyListeners to using key binding. How to use Key Binding
Similar Threads
-
Help with keylistener?
By Kaizo in forum New To JavaReplies: 4Last Post: 12-11-2010, 12:55 AM -
AWT KeyListener Problem
By plm-pusik in forum New To JavaReplies: 15Last Post: 11-10-2010, 03:38 PM -
KeyListener problem
By siyi90 in forum AWT / SwingReplies: 7Last Post: 02-08-2010, 10:16 AM -
Problem with KeyListener in Jtable
By sandeepsai17 in forum New To JavaReplies: 0Last Post: 06-30-2009, 10:14 AM -
KeyListener - Is this what I need?
By dbashby in forum New To JavaReplies: 26Last Post: 04-18-2009, 04:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks