Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
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  
  shooting delays and movement issues  (Read 783 times)
0 Members and 1 Guest are viewing this topic.
Offline Zero Volt

Senior Newbie


Exp: 4-6 months



« Posted 2014-07-17 21:07:32 »

Good evening gamers!
I have 2 problems:
Problem 1:
when I hold press the space key to shoot, 2 things happen:
          - at first it shoot one bullet and then it stops for about a second and then it continue shooting the other bullets
          - while I'm shooting the bullets if I pressed the down or up keys to move the spaceship it stops shooting.

problem 2:
if I pressed the right arrow key to move the spaceship and then I quickly pressed the left arrow key the spaceship stops
for about a second and it continue moving to the left.

how can I solve these 2 problems ? what I want is this:
1. I want the moment I press the space key to shoot, it start shooting immediately bullet after the other.
2. I want to be able to shoot while I'm moving the ship.
3. I want to be able to move smoothly right and left without any delays.

here's my 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  
public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_SPACE) {
            fire();
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 2;
        }

        if (key == KeyEvent.VK_UP) {
            dy = -2;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 2;
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = -2;
        }
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }
    }



And here's a link to download the game in a jar extension + the source code :
https://www.mediafire.com/?ebc5okuk2uk0g05


Offline danieldean
« Reply #1 - Posted 2014-07-17 21:38:25 »

The repeating of the key press is the same as what you get when typing. Try pressing a key in a text field, notepad or whatever and you'll see.

It's not clear if your using and engine or Java 2D and custom coding, I don't want to get it all from MediaFire, could you put it on the the pastebin (http://pastebin.java-gaming.org/). I think you may need to use polling to get a constant input, at least this is what I have done, see this: http://pastebin.java-gaming.org/7fb059e290315

You can then use:

1  
2  
3  
if (keyInput == KeyEvent.VK_SPACE) {
    fire();
}


In your game loop. You need to use:

1  
KeyInput keyInput = KeyInput.getInstance();


And:

1  
addKeyListener(keyInput);


To use this. If it's of any use to you feel free to do as you wish with the code, it's fairly generic. It should solve all the problems you've listed used properly.

Offline Zero Volt

Senior Newbie


Exp: 4-6 months



« Reply #2 - Posted 2014-07-17 21:49:27 »

Thanks daniel for replaying, here's the code :
http://www.java-gaming.org/?action=pastebin&id=1030
files in the link goes like this: Window.java, Screen.java, SpaceShip.java, Bullet.java, and AlienShip.java

and I'm using java Graphics2D
I'm gonna go now and try what you wrote
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline danieldean
« Reply #3 - Posted 2014-07-17 22:01:20 »

I'm going to also suggest you have a look at Game loops!. Not that your approach won't work just that it probably isn't 'best'.
Offline Zero Volt

Senior Newbie


Exp: 4-6 months



« Reply #4 - Posted 2014-07-17 22:06:59 »

I'm not sure what fragment in my code the loop is happening !
any idea  ?
Offline danieldean
« Reply #5 - Posted 2014-07-17 22:11:05 »

You don't have a loop... you've a timer hence why I gave you that link. I've never used a timer for anything I've done but I think you'd use the code within:

1  
2  
3  
4  
5  
6  
public void actionPerformed(ActionEvent ae) {
    // Game logic and painting?
    if (keyInput == KeyEvent.VK_SPACE) {
        fire();
    }
}


Rather than do this though I'd suggest you read through the link and consider rewriting to use a loop, it should take too much to do.
Offline Zero Volt

Senior Newbie


Exp: 4-6 months



« Reply #6 - Posted 2014-07-18 00:21:01 »

after changing this code:

1  
2  
3  
if(keyInput == KeyEvent.VK_SPACE){
           
}


to this(I believe this is what you meant ?)

1  
2  
3  
if (keyInput.getKeyState(KeyEvent.VK_SPACE)) {
    fire();
}


your class KeyInput is working perfectly ! with all the problems are solved !

there's just one more thing, when I use your class the bullets are way too fast ! it now looks like a laser !  Clueless  instead of being separated bullets. I think this is related to the game loop you were talking about right ?

Is there any chance to slow the frames down while keeping my code the same using timers ?
Offline thedanisaur
« Reply #7 - Posted 2014-07-18 01:52:44 »

The problem with timers is that, iirc, it doesn't keep consistent timing, so even if you slow it down it may seem like it's working correctly and then occasionally it will run slower (maybe faster as well?).

Anyway I'm pretty sure that when you call new Timer() you can change 5 to something larger and it will slow down the game, but I wouldn't recommend this approach either.

Here's another good article, but it may be over your head as it's pretty in-depth.

http://gafferongames.com/game-physics/fix-your-timestep/

Also, your logic "loop" is actually actionPerformed().

Every village needs an idiot Cool
Offline Zero Volt

Senior Newbie


Exp: 4-6 months



« Reply #8 - Posted 2014-07-18 03:06:47 »

Actually I did tried to put a larger number than 5 but the issue is with the frames, there is too much frames are being rendered no matter how much I change the timer, the bullets should be separate but they're connected instead.

EDIT:

I fixed it by doing this(Just temporary, I know this is not the professional way)

1  
2  
3  
4  
5  
6  
7  
8  
int delay = 0;
++delay;
while (delay > 60) {
    delay = 0;
     if (keyInput.getKeyState(KeyEvent.VK_SPACE)) {
         spaceShip.fire();
    }
}


Now I can see the bullets !
Note: never mind the variable 'delay' I know it doesn't make sense!
Offline thedanisaur
« Reply #9 - Posted 2014-07-18 04:01:47 »

That...hurts my soul. haha. It's OK if it works it works.

What you might consider doing though is taking your game logic out of the draw loop, paint(), and putting that logic in your actionPerformed(). The reason being that when you change your timer interval it should slow down the gameplay, the bullets shooting, ships moving, etc, but it won't effect the drawing.

edit: OK I looked at the code again and you call repaint in your actionPerformed() so that's where your drawing and logic are connected.

Every village needs an idiot Cool
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline danieldean
« Reply #10 - Posted 2014-07-18 08:23:40 »

to this(I believe this is what you meant ?)

1  
2  
3  
if (keyInput.getKeyState(KeyEvent.VK_SPACE)) {
    fire();
}


Is there any chance to slow the frames down while keeping my code the same using timers ?

Yes that is what I meant, sorry it was late and my brain ceased functioning. I'd still suggest you separate up your logic and rendering though using a loop to get decent time control. Note that your fix could be:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
// Outside loop or timer somehow.
int delay = 0;

// Inside loop or timer.
if(keyInput.getKeyState(KeyEvent.VK_SPACE)) {
    if(++delay > 60) {
        delay = 0;
        spaceShip.fire();
    }
}


Which I think is a better alternative. This does mean your initial shot is delayed. Maybe you'd want to start at 60 to rectify this.
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (31 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (40 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (26 views)
2014-10-26 10:14:04

theagentd (32 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!