Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: KeyListener probs, noo not another one on: 2005-10-27 13:22:19
Quote
Simply call

setFocusable(true)
Thankyou, thankyou!! I was seriously going mental over that!!! <-- i thought it would be simple

Quote
requestFocus() and setVisible() are things that you generally don't call in a constructor
Would you believe that DrawingFrame actually came from my computer science lecturer, and has been in the course notes for prob 5 years!! So if i've learnt bad programming blame those guys!

Quote
You should probably stick to the Java coding convention of starting identifier names with a lowercase letter and class names with an uppercase letter.
I thought i was pretty good with this, the only time i didn't do it was with the instance of HumanPlayer (i named it Player).

Quote
You've construction code scattered all over your source files, much of it outside of constructors.
I'm not sure what you mean by this? I can't see what i'm doing wrong, but then i was taught to do it like this, so please tell me.

Quote
You have physics/player state updates inside paintComponent, instead of in your main game loop.
I know this is wrong, but i was just doing sort of a hack-and-slash to try and get the thing to actually move. Won't happen again Cheesy

Quote
You have methods named "pressedKey" and "releasedKey" in your player class, which is really a UI thing.  You really want methods to set the player's direction of acceleration - call those methods from your KeyListener if you like.
Makes sense, I'll try to call it something different next time. The only thing was i wanted to process the KeyEvents inside the HumanPlayer class, but probably is better to do it in the game class.

@demonpants: Thanks for the info, but i'm not totally up with the focus yet, so i think i'll have to keep reading you're post to make sense of it!


Thanks every1 you've been tons of help, i'm sure i'll more problems in a day or two! Should i make a new thread if i do or not?
2  Game Development / Newbie & Debugging Questions / Re: KeyListener probs, noo not another one on: 2005-10-26 23:36:03
AAAAAAARrrrrrggghhhhhhhh!!!!! This has to be one of the most simple things, but i can't get it right!! I feel that i've tried everything!!

I think the problem is in the KeyListener or in the addKeyListener, but i'm not sure what!!

I've included all my source code in this post, just in one txt file, so u'll have to break it up (Sorry i wasn't sure what
the convention was). But if you have a spare 5 mins or so, could you have a peek.

Or here's my new GameFrame.java:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
import javax.swing.*; // For JPanel, etc.
import java.awt.*; // For Graphics, Color etc.
import java.awt.geom.*; // For Rectangle2D, etc
import java.awt.event.*;
public class GameFrame extends JPanel {
   
   // the angle generated
   double x = 300;
   double y = 300;
   
   HumanPlayer Player = new HumanPlayer();
   // and main circle
   
   // constructor
   public GameFrame(){
      requestFocus();
      addKeyListener(listener);
   }
   KeyListener listener = new KeyListener(){
      public void keyPressed(KeyEvent ke){
         System.out.println("Key is pressed");
         Player.pressedKey(ke);
      }
      public void keyTyped(KeyEvent ke){};
      public void keyReleased(KeyEvent ke){
         Player.releasedKey(ke);
      }
   };
   
   public void paintComponent(Graphics g){
     
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D)g;
     
      //
      g2d.setColor(Color.black);
     
      y = y+Player.getVel();
      Rectangle2D.Double player = new Rectangle2D.Double(x, y, 10, 30);
     
      g2d.draw(player);
   }
}
Maybe there's something inherently wrong in there i'm not picking up!

Thanks for all the quick replies
Dr Wam
3  Game Development / Newbie & Debugging Questions / Re: KeyListener probs, noo not another one on: 2005-10-26 02:04:58
Yes getVel() is called by a Game class that contains an instance of HumanPlayer, every 30 milliseconds (every redraw).

I've used this println thing before, so it should work in the switch statement.

1  
2  
3  
4  
5  
6  
Alarm bells started when I got to the first line of code  

Quote
public class HumanPlayer extends JFrame

That is most definitely not an 'is a' relationship


Um ok, i wasn't sure. But if i don't 'extends JFrame' i can't use addKeyListener anywhere!!!
Can u please explain how to get around this!!
4  Game Development / Newbie & Debugging Questions / KeyListener probs, noo not another one on: 2005-10-25 07:26:02
Man i just cannot get this to work, could u please help!!
What am i doing wrong:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;

public class HumanPlayer extends JFrame{
   
   //
   double velocity = 0;
   double speed = 5;
   boolean upkey, downkey, leftkey, rightkey;
   
   public HumanPlayer(){
      addKeyListener(listener);
   }
   
   public double getVel(){
      if(upkey){
         velocity += speed;
      } else if(downkey){
         velocity -= speed;
      }
      velocity = velocity * .98;
     
      return velocity;
   }
   /*
   addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent e){
         System.exit(0);
      }
   });
   */
 
   
   KeyListener listener = new KeyListener(){
      public void keyPressed(KeyEvent e){
         int code = e.getKeyCode();
         
         switch(code){
            case KeyEvent.VK_UP: upkey = true; System.out.println("Up pressed"); break;
            case KeyEvent.VK_LEFT: leftkey = true; break;
            case KeyEvent.VK_RIGHT: rightkey = true; break;
            case KeyEvent.VK_DOWN: downkey = true; break;
         }
         
      };
     
      public void keyTyped(KeyEvent e){};
     
      public void keyReleased(KeyEvent e){
         int code = e.getKeyCode();
         
         switch(code){
            case KeyEvent.VK_UP: upkey = false; System.out.println("Up released"); break;
            case KeyEvent.VK_LEFT: leftkey = false; break;
            case KeyEvent.VK_RIGHT: rightkey = false; break;
            case KeyEvent.VK_DOWN: downkey = false; break;
         }
         
      }
   };
}
I can't get the keyListener to work, i must just be doing something wrong then?
I can give the other files if u need them. I'm just trying to get a box to move around atm.
Thanks,
        Dr Warm
Pages: [1]
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (54 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (85 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (79 views)
2014-12-05 12:39:02

SHC (89 views)
2014-12-03 16:27:13

CopyableCougar4 (97 views)
2014-11-29 21:32:03

toopeicgaming1999 (155 views)
2014-11-26 15:22:04

toopeicgaming1999 (153 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!