Jump to content
  • Advertisement

someguy127

Member
  • Content Count

    3
  • Joined

  • Last visited

Community Reputation

101 Neutral

About someguy127

  • Rank
    Newbie
  1. Hi, I am working on one of my first games in java and I was having a big problem with the Event Handler where it would only work somewhere in the realm of 1/10 times. If anyone has any idea on how to solve this problem I'd be much obliged. This is my game panel constructor, I have global variables called isRight, isDown etc... and in the main gameloop I check if they are true in which case my character moves so many pixels public GamePanel(){ setFocusable(true); requestFocus(); setPreferredSize(new Dimension(GWIDTH,GHEIGHT)); makeGrid(); setBackground(Color.white); addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_RIGHT: isRight = true; System.out.println("Rightpressed"); break; case KeyEvent.VK_LEFT: isLeft = true; break; case KeyEvent.VK_UP: isUp = true; break; case KeyEvent.VK_DOWN: isDown = true; default: break; } } public void keyReleased(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_RIGHT: isRight = false; System.out.println("Rightreleased"); break; case KeyEvent.VK_LEFT: isLeft = false; break; case KeyEvent.VK_UP: isUp = false; break; case KeyEvent.VK_DOWN: isDown = false; default: break; } } public void keyTyped(KeyEvent e) { } }); } This here is the update methods and run() game loop, I don't think that the problem is in here because I put in a print statement in each of these methods and they ran consistently. public void gameUpdate() { //update game logic if(isRight){ player.x += player.speed; } if(isLeft){ player.x -=player.speed; } } public void run() { while(running) { gameUpdate(); gameRender(); paintScreen(); try{ game.sleep(20); } catch(Exception e){} } } And Just incase it is not a problem with my constructor, here is my complete code if anyone is willing to look through it import javax.swing.*; import java.awt.*; import java.awt.event.*; public class GamePanel extends JPanel implements Runnable{ /** * */ private static final long serialVersionUID = 1L; //double buffering Image dbImage; Graphics dbg; Rectangle[] grid = new Rectangle[250]; //JPanel Vars public static final int GWIDTH = 500, GHEIGHT = 500; static final Dimension gameDim = new Dimension(GWIDTH,GHEIGHT); public boolean isRight = false,isLeft = false, isUp = false,isDown=false; private Thread game; private volatile boolean running = false; Player player = new Player(); public GamePanel(){ setFocusable(true); requestFocus(); setPreferredSize(new Dimension(GWIDTH,GHEIGHT)); makeGrid(); setBackground(Color.white); addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_RIGHT: isRight = true; System.out.println("Rightpressed"); break; case KeyEvent.VK_LEFT: isLeft = true; break; case KeyEvent.VK_UP: isUp = true; break; case KeyEvent.VK_DOWN: isDown = true; default: break; } } public void keyReleased(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_RIGHT: isRight = false; System.out.println("Rightreleased"); break; case KeyEvent.VK_LEFT: isLeft = false; break; case KeyEvent.VK_UP: isUp = false; break; case KeyEvent.VK_DOWN: isDown = false; default: break; } } public void keyTyped(KeyEvent e) { } }); addMouseListener(new MouseListener(){ public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } }); } private void makeGrid(){ int x=0,y=0; for(int i = 0; i <grid.length;i++) { if(i % 16 != 0 | i ==0){ grid = new Rectangle(x,y,32,32); x+=32; } else{ y+=32; x = 0; grid = new Rectangle(x,y,32,32); x+=32; } } } private void startGame() { if(game == null || !running){ running = true; game = new Thread(this); game.start(); } } public void stopGame() { if(game != null || running) { running = false; } } public void addNotify() { super.addNotify(); startGame(); } //Game Rendering public void gameUpdate() { //update game logic if(isRight){ player.x += player.speed; } if(isLeft){ player.x -=player.speed; } } private void gameRender() { if(dbImage == null) { dbImage = createImage(GWIDTH,GHEIGHT); if(dbImage == null) { System.err.println("Error in Game Render"); return; } else { dbg = dbImage.getGraphics(); } } dbg.setColor(Color.WHITE); dbg.fillRect(0, 0, getWidth(),getHeight()); //drawGAME draw(dbg); } public void draw(Graphics g) { //draw things here g.setColor(Color.black); //player.draw(g); g.fillRect(player.x, player.y, player.width, player.height); g.setColor(Color.RED); for(int i = 0; i < grid.length; i++){ g.drawRect(grid.x, grid.y, grid.width, grid.height); } } public void paintScreen() { Graphics g; try{ g = this.getGraphics(); if(dbImage != null && g!= null){ g.drawImage(dbImage,0,0, null); } Toolkit.getDefaultToolkit().sync(); g.dispose(); } catch(Exception e) { System.err.println("Error in paintScreen"); } } //logic public void run() { while(running) { gameUpdate(); gameRender(); paintScreen(); try{ game.sleep(20); } catch(Exception e){} } } }
  2. Hey everyone I am new here, and I have begun the wonderful journey of being a game programmer. first a little background, I am currently a college student studying Computer Science. I love programming, and more recently I love game programming. Unfortunately my school doesn't teach game programming as part of the core Computer Science Major so I have been tasked with learning this art on my own. What I have learned so far is the basic idea of Game loops, Movement, Collision etc.. just basic things you pick up from online tutorials. My languages so far have been Java, and C# with the XNA game library, I know the basics of both but when it comes to game programming I use C# because XNA takes care of the Visual aspect of it. Well any way on to the main point of this topic, I know that every language has it's benefits and disadvantages, and I am looking for the ones that I would like to work with. So if you guys would be so kind as to pick some languages and give me the pros and cons of using it to make games, that would be great. Also if there are any basic concepts of game programming I need to learn, please let me know. Thanks a ton -Someguy127
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!