Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Moving a ball with Arrow Keys  (Read 2684 times)
0 Members and 1 Guest are viewing this topic.
Offline kekcklemen

Junior Newbie





« Posted 2011-02-25 20:59:57 »

Ok guys. I'm making a game. For now I made a program in which you can move ball with arrow keys. UP, DOWN, RIGHT and LEFT.

My first question is:
I clicked one button... Ball moves... but not instantly!! I just hate this. Like it's somehow delayed or something. At key click it moves only for 5 sources than it moves normally. But I want that ball moves constantly... Try it out with my code. How to make ball moving nice and without dalaying?

My second question is:
I can move left and right, up and down.  how to move up and left at the same time? when I click Up and Left keys I want that ball moves up and left. not just left or just up. how to do that?

here is the code:


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  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.awt.event.*;
import javax.swing.*;

public class HandlingEvents implements Runnable {

    JFrame frame;
    int myX = 400;
    int myY = 400;
    Canvas canvas;
    BufferStrategy bufferStrategy;
    boolean running = true;

    public HandlingEvents() {
        frame = new JFrame("Basic Game");
        JPanel panel = (JPanel) frame.getContentPane();
        panel.setPreferredSize(new Dimension(500, 500));
        panel.setLayout(null);
        canvas = new Canvas();
        canvas.setBounds(0, 0, 500, 500);
        canvas.setIgnoreRepaint(true);
        panel.add(canvas);
        canvas.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent evt) {
                moveIt(evt);
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setResizable(false);
        frame.setVisible(true);
        canvas.createBufferStrategy(2);
        bufferStrategy = canvas.getBufferStrategy();
        canvas.requestFocus();
    }
    public void run() {
        while (running = true) {
            Paint();
            try {
                Thread.sleep(25);
            } catch (InterruptedException e) {
            }
        }
    }
    public static void main(String[] args) {
        HandlingEvents ex = new HandlingEvents();
        new Thread(ex).start();
    }
    public void Paint() {
        Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
        g.clearRect(0, 0, 500, 500);
        Paint(g);
        bufferStrategy.show();
    }

    protected void Paint(Graphics2D g) {
        g.fillOval(myX, myY, 30, 30);
    }
    public void moveIt(KeyEvent evt) {
     switch (evt.getKeyCode()) {
            case KeyEvent.VK_DOWN:
                myY += 5;
                break;
            case KeyEvent.VK_UP:
                myY -= 5;
                break;
            case KeyEvent.VK_LEFT:
                myX -= 5;
                break;
            case KeyEvent.VK_RIGHT:
                myX += 5;
                break;
        }
    }
}



pls help =/
Offline aazimon
« Reply #1 - Posted 2011-02-25 21:21:04 »

What's happening is the OS only allows one key to fire the event at a time. When you hold down one key then press a second one, it stops calling the fire for the first one.
You need to keep track of which button is pressed, by setting a boolean. Then add the keyReleased to your KeyAdapter. Set the boolean to true on the key press and false on the key release. In your paint you will have to add to your movement. See this post a while back.
http://www.java-gaming.org/topics/trouble-getting-started/23664/msg/196970/view.html#msg196970
Offline kekcklemen

Junior Newbie





« Reply #2 - Posted 2011-02-25 21:25:20 »

What's happening is the OS only allows one key to fire the event at a time. When you hold down one key then press a second one, it stops calling the fire for the first one.
You need to keep track of which button is pressed, by setting a boolean. Then add the keyReleased to your KeyAdapter. Set the boolean to true on the key press and false on the key release. In your paint you will have to add to your movement. See this post a while back.
http://www.java-gaming.org/topics/trouble-getting-started/23664/msg/196970/view.html#msg196970
ok ty i'll try this. Do you know how to solve problem with delayed painting?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline aazimon
« Reply #3 - Posted 2011-02-26 00:12:19 »

Once you implement the boolean method this will fix the pausing when you first press the button.
Offline deadly72

Junior Member





« Reply #4 - Posted 2011-02-26 07:07:37 »

Once you implement the boolean method this will fix the pausing when you first press the button.

I'd actually like to know why this would fix the delay?  Is it because he's not using the standard keyPressed/keyReleased instead of his method?  I actually doubt that would be the problem, but I can't see the reason as to why it would be delayed on his first key press.
Offline zoto

Senior Member


Medals: 4



« Reply #5 - Posted 2011-02-27 00:47:55 »

He is using keyPressed just using it in a strange way, see line 28.
The reason it causes such odd behavior is because the keyPressed is only called at the computers typematic rate and also it's called from a different thread, the boolean method described above fixes both of these.

On a side note I noticed an infinite loop bug on line 41, your missing an = or better yet remove the comparison because running is already a boolean.
Offline ReBirth
« Reply #6 - Posted 2011-02-27 05:04:25 »

For 2nd question, use help of dx and dy variabel and update your ball position through these variabels. They will control the movement direction and speed. If you make your game loop right, it should be moving diagonal.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

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

The first screenshot will be displayed as a thumbnail.

ctomni231 (32 views)
2014-07-18 06:55:21

Zero Volt (28 views)
2014-07-17 23:47:54

danieldean (24 views)
2014-07-17 23:41:23

MustardPeter (25 views)
2014-07-16 23:30:00

Cero (40 views)
2014-07-16 00:42:17

Riven (42 views)
2014-07-14 18:02:53

OpenGLShaders (29 views)
2014-07-14 16:23:47

Riven (29 views)
2014-07-14 11:51:35

quew8 (26 views)
2014-07-13 13:57:52

SHC (63 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!