Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (683)
Games in Android Showcase (196)
games submitted by our members
Games in WIP (751)
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: Moving a ball with Arrow Keys on: 2011-02-25 20: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?
2  Game Development / Newbie & Debugging Questions / Moving a ball with Arrow Keys on: 2011-02-25 19: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 =/
Pages: [1]
 
orrenravid (202 views)
2016-07-16 03:57:23

theagentd (274 views)
2016-07-11 14:28:54

Hydroque (358 views)
2016-07-06 05:56:57

Hydroque (514 views)
2016-07-03 08:52:54

GrandCastle (385 views)
2016-07-01 09:13:47

GrandCastle (379 views)
2016-07-01 09:09:45

CopyableCougar4 (432 views)
2016-06-25 16:56:52

Hydroque (409 views)
2016-06-22 02:17:53

SwampChicken (374 views)
2016-06-20 13:22:57

SwampChicken (301 views)
2016-06-20 13:22:49
Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42

FPS Camera Tutorial
by Hydroque
2016-05-22 05:40:58

Website offering 3D Models specifically for games for free
by vusman
2016-05-18 17:23:09

Website offering 3D Models specifically for games for free
by vusman
2016-05-09 08:50:56

Website offering 3D Models specifically for games for free
by vusman
2016-05-06 11:10:21

Website offering 3D Models specifically for games for free
by vusman
2016-04-29 12:56:17
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!