• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

This topic is now archived and is closed to further replies.

greensam

[java] Keyboard Input

6 posts in this topic

hello. i have a question about keyboard input, now i might screw up a bit in my explanations since my isdn card has some problems so i have to surf at my brother''s computer and out network between them also has some problems so i cant paste in parts of the source except from my memory. now i am trying to create a simple space invaders clone just to learn stuff. i''ve got a thread set up that draws a background on a frame and a space ship on it. then i tried adding a keylistener which extends keyadapter (i might have confused these two names here) to the frame. my keyadapter class looks something like this: (can be errors here due to me having to run back and forth between two rooms remembering the lines of code in my head) public void keyPressed (keyEvent e) { if (e.getKeyCode() == e.VK_KP_LEFT) ship.moveleft(); else if (e.getKeyCode() == e.VK_KP_RIGHT) ship.moveright(); } this passes through the compiler but the ship won''t move at all. i''d be very grateful for any help thanks sam
0

Share this post


Link to post
Share on other sites
Well for one thing you only get one event per keypress, so it might be moving, just a tiny ammount. I''m assuming that top have properly done a frame.addKeyListener(this?).

Really what you should be doing is more like..

public void keyPressed (keyEvent e) {
if (e.getKeyCode() == e.VK_KP_LEFT ) ship.moveleft();
else if (e.getKeyCode() == e.VK_KP_RIGHT ) ship.moveright();
}

public void keyReleased (keyEvent e) {
if (e.getKeyCode() == e.VK_KP_LEFT ) ship.stop();
else if (e.getKeyCode() == e.VK_KP_RIGHT ) ship.stop();
}

ship.moveleft and moveright should set a variable to indicate that it is moving. When you redread and it is set move it left or right, the stop command clears the movment.

The reason for the change is the key pressed ( as opposed to keyType event ) is that it is only sent ONCE when the person depresses the key. This is why to need to trap both otherwise you would never know when the key was released. If you just want what would have showed up in a text window "a..a..aaaaaa" capture the keyTyped event and not the keyPressed event.

Cheers


0

Share this post


Link to post
Share on other sites
Thanks a lot for taking your time to help me out, but you have in this case helped me solve a problem i didn''t have yet.

I am not yet concerned with making input work great but make it work AT ALL. I added a drawString to my thread so that it writes the coordinates of the player sprite to check if they change at all but they don''t.

So I have done the:

class playercontrol extends KeyAdapter () {

here i have the methods that check for input just like you proposed they should be

}

container.addKeyListener(control);

and this all passes through the compiler.

It''s strange. I think it should work, because I''ve managed to add a WindowAdapter too which checks for windowClosing and does a System.exit(0); so I think I''ve sorted out how it''s supposed to work when adding inpurtlisteners.

very grateful for further help.

sam
0

Share this post


Link to post
Share on other sites
Check these two functions to see if it helps any...

Component.requestFocusInWindow()
Component.enableEvents(mask)

and

KeyboardFocusManager.setGlobalFocusManager(compnent)

I''m sure the events are get getting redirected, do you have any other components in your windows that might be capturing events?
0

Share this post


Link to post
Share on other sites
Thanks again for your help.

I have already sorted out part of the problem though. It seems that the fields VK_KP_LEFT and VK_KP_RIGHT in KeyEvent didn''t corresponf to my keyboard''s left and right arrow keys. I tried changing the controls to a and d. Well it worked. Now thanks to you it works pretty well. At least much better than it would have done the way i had it before when it would have only moved one pixel per keypress.

The problem is, it''s still a bit sloppy. For example if you push right first, then start pushing left immediately after you release right (perhaps in the same "execution-cycle" of the thread) the ship stops for a short while before changing direction.

And if you push right then start pushing left while you push right it changes direction smoothly. But if you then let go of the left key it stops and fails to notice that you haven''t lifted your finger off the right key.

Sam
0

Share this post


Link to post
Share on other sites
The last one is simple to fix.

public void keyReleased (keyEvent e) {
if (e.getKeyCode() == e.VK_KP_LEFT && ship.movingleft) ship.stop();
else if (e.getKeyCode() == e.VK_KP_RIGHT && ship.movingright ) ship.stop();
}

So it only stops movement if the key released is the direction it''s moving in. By chance is your keyboard international? You could always set up a little keyapp to show you what keys it THINKS are being pressed.

0

Share this post


Link to post
Share on other sites
Check these two functions to see if it helps any...

Component.requestFocusInWindow()
Component.enableEvents(mask)

and

KeyboardFocusManager.setGlobalFocusManager(compnent)


Is all of that needed? Surely all you need is to call requestFocus()?

Also VK_KP_LEFT/VK_KP_RIGHT or for the the key pad (th bit on the right with all of the numbers), not the arrow kes. Use VK_LEFT/VK_RIGHT instead. Particulary because not all keyboards have a keypad with arrows on, but nearly all have arrow keys.

cheers,
John
0

Share this post


Link to post
Share on other sites