CodeWalrus

General => General Help & Troubleshooting => Topic started by: p2 on November 13, 2016, 06:51:10 pm

Title: Keyboard inputs in Java ( WalrusKingdom ) [Java]
Post by: p2 on November 13, 2016, 06:51:10 pm
Hi guys,
I'm currently working on my WalrusKingdom (former PocketWalrus) game and ran into problems...
meaning I'm totally stuck here and dont know what to do :(

thanks to @c4ooo and @kotu I have my game running and the map is displayed.
But the key inputs won't work.
As I added key inputs, they worked but the rendering stoped working (without an error, simply stops showing any graphics).
Putting the key input stuff in comments again, the graohics come back.
(Always only one of them is working).
I really hope someone can help me on this as I'm stuck here since over 2 weeks now :(

the rendering code as well as the keyboard code are both perfectly working but they seem to overwrite each other or not play together very well or something like that, idk.
My github project: https://github.com/KaliPhobos/WalrusKingdom
The key input method I tried to implement: http://codewalr.us/1649/46276 (http://codewalr.us/1649/46276)
The WINDOW in the main file containing the MAIN class. As you can see I out the keyboard inouts in comments right now.
When trying to run my project, make sure you have my picture in a special folder: "/resources/tiles.png"
Thank you very much, I'd really appreciate any help here... <3
Title: Re: Keyboard inputs in Java ( WalrusKingdom ) [Java]
Post by: kotu on November 13, 2016, 10:32:33 pm
hi p2 i got it working :)

here is your new main()....

public static void main(String[] args) {
LoadBlocks(); // Loading background block data
Screen.setSize(width/blocksize, height/blocksize); // Setup Screen (results in 0-11 x 0-17)
JFrame window = Screen.createWindow(); // Create window object (JFrame)

System.out.println("hello");
Keys keys = new Keys(window);
window.pack();
window.setVisible(true);

Map.setSize(100, 100); // Create map with given size
//Player Walrii = Player.createPlayer(0, 10, 10); // Define Player
TileArea tileArea = Screen.createTileArea(window);
Player.setXPos(0);
Player.setYPos(22);
Map.createRandom(0, 96);
Map.loadCity1();
Screen.update();
Screen.render(true);
while (true) { // MAIN GAME LOOP
    Screen.update();
    Screen.render(true);
    Screen.UpdateOldData();
    window.repaint();
    //KeyControls.checkInput();
    Keys.checkInput();
    General.sleep(10);
}
}


Here is the new file Keys.java.......

package CodeW;

import javax.swing.JFrame;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Keys implements KeyListener
{
public static boolean keyDown[];

public void keyPressed(KeyEvent ke)
{
keyDown[ke.getKeyCode()] = true;
}
public void keyReleased(KeyEvent ke)
{
keyDown[ke.getKeyCode()] = false;
}
public void keyTyped(KeyEvent ke)
{
}

public Keys(JFrame _window)
{
keyDown = new boolean[65536];
_window.addKeyListener(this);
}

public static void checkInput()
{
for (int _x=0;_x<65536;_x++)
{
        if (keyDown[_x] == true) {
        System.out.println(_x+" pressed");
        }
        }
}
}


Note there is more I want to do this, particularly I don't think the key array size should be 65536, but I will look at it later. Enjoy!
Title: Re: Keyboard inputs in Java ( WalrusKingdom ) [Java]
Post by: p2 on November 13, 2016, 10:41:44 pm
okeyyyy thanks to kotu IT WORKS NOW!!! Thank you so much! :D :D
Next thing is implementing correct walking (working on that) as well as a modified rendering mechanism (only rendering changes and stuff) ^^
thx a lot :D <3
Title: Re: Keyboard inputs in Java ( WalrusKingdom ) [Java]
Post by: kotu on November 13, 2016, 10:42:43 pm
Quote from: p2 on November 13, 2016, 10:41:44 pm
as well as a modified rendering mechanism (only rendering changes and stuff) ^^


i wouldn't do that it's very difficult and not a robust technique.
Title: Re: Keyboard inputs in Java ( WalrusKingdom ) [Java]
Post by: p2 on November 13, 2016, 10:57:06 pm
I call the render method based on a fixed rate (modulo of milliseconds) but then fasten the method like that.
actually it' pretty easy, was already using it in my AutoIt version of the game ^^ (And its already partially embedded here, but not functional yet) ^^
Title: Re: Keyboard inputs in Java ( WalrusKingdom ) [Java]
Post by: kotu on November 13, 2016, 10:58:45 pm
what's the framerate currently? i think it's pretty fast
Title: Re: Keyboard inputs in Java ( WalrusKingdom ) [Java]
Post by: p2 on November 13, 2016, 10:59:55 pm
for whatever reason it's pretty unstable... :/
But I'll add a framerate counter once I'm done with the modified render algorythms :)
Title: Re: Keyboard inputs in Java ( WalrusKingdom ) [Java]
Post by: kotu on November 14, 2016, 03:55:26 am
ok @p2 .... i checked out the values of the keycode and on  my keyboard they go from 8 to 525

i therefore changed the Keys.java file array size to be 1024

i used a constant (defined in java using the final keyword) for this, and added some bounds checking to make the code safe

here is the new file.


package CodeW;

import javax.swing.JFrame;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Keys implements KeyListener
{
private static final int NUM_KEYS_STORED = 1024;
public static boolean keyDown[];

public void keyPressed(KeyEvent ke)
{
if (ke.getKeyCode() < NUM_KEYS_STORED) {
keyDown[ke.getKeyCode()] = true; // 8 to 525
}
}
public void keyReleased(KeyEvent ke)
{
if (ke.getKeyCode() < NUM_KEYS_STORED) {
keyDown[ke.getKeyCode()] = false;
}
}
public void keyTyped(KeyEvent ke)
{
}

public Keys(JFrame _window)
{
keyDown = new boolean[NUM_KEYS_STORED];
_window.addKeyListener(this);
}

public static void checkInput()
{
for (int x = 0; x < NUM_KEYS_STORED; x++)
{
                if (keyDown[x] == true) {
                System.out.println(x + " pressed");
                }
                }
}
}



*edit*
also just noticed this...........
http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#KEY_FIRST (http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html#KEY_FIRST)