Electron Golf 4k

Thanks for adding the arrow keys. And it was not necessary to ask you twice to get them !

Still runs fine on my Internet Explorer 9 and Google Chrome under Windows 7, WASD as arrow keys and spacebar.

Concerning your source, if the key problem lasts for ra4king and Shazer 2, I was asking for the frame with only the handling key part detailed.

This is what the Keyboard handlers look like now:

@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
case KeyEvent.VK_W:
up = true;
break;
case KeyEvent.VK_DOWN:
case KeyEvent.VK_S:
down = true;
break;
case KeyEvent.VK_LEFT:
case KeyEvent.VK_A:
left = true;
break;
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_D:
right = true;
break;
case KeyEvent.VK_SPACE:
space = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
case KeyEvent.VK_W:
up = false;
break;
case KeyEvent.VK_DOWN:
case KeyEvent.VK_S:
down = false;
break;
case KeyEvent.VK_LEFT:
case KeyEvent.VK_A:
left = false;
break;
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_D:
right = false;
break;
case KeyEvent.VK_SPACE:
space = false;
break;
}
}

What component do you add the listener to? The applet itself? Do you add any other components on the applet? Try adding a call to “requestFocus()” right before your loop starts.

This is my event function…
(There are some good sources here to look for ideas to make it short)


public boolean handleEvent(Event ev)
	{
		boolean pressed = (ev.id==403 || ev.id==401) ?true:false;
		if(ev.id>=401 && ev.id<=404) keys[ev.key] = pressed;
		return true;
	}

in the gameloop used like this: (Ok, a bit unclean, you can read out the keycode in ev.key)


if (keys[keyFire] || keys[119] || keys[keyUp]) comJump = true;
if (keys[keyRight] || keys[100]) comRight = true;
if (keys[keyLeft] || keys[97]) comLeft = true;
if(keys[keyDown] || keys[115]) comDown=true;

-> and DONT! use switch case constructs, they make a much larger bytecode than a simple if chain.


public class A extends JApplet implements ActionListener, KeyListener, MouseListener, MouseMotionListener{
	...
	public A() throws HeadlessException {
		...
		space = up = down = left = right = false;
		addKeyListener(this);
		addMouseListener(this);
		addMouseMotionListener(this);
		...
	}

That’s what it looks like right now. The listeners are added to the Applet itself and no other components are added. I just added a call to requestFocus() in my constructor, so give it a try.

I’m at like 3.29 kB so I’ve got space to BS around with (hence the switch/case).

if you use the event function directly, you dont need any listeners.

That’s how I had it before, but it wasn’t working for some people :frowning:

Try out this skeleton


import java.applet.Applet;
import java.awt.Event;

public class S extends Applet implements Runnable
{


	final private boolean[] keys = new boolean[32767];
	private static final int keyUp = 1004;
	private static final int keyDown = 1005;
	private static final int keyLeft = 1006;
	private static final int keyRight = 1007;
	private static final int keyFire = 32;
	
	
	public void init()
	{
		new Thread(this).start();
	}

	public void start()
	{
		setSize(800, 600);
	}


	
	
	public void run()
	{

		//wait for activation
		while (!isActive())
		{
			Thread.yield();
		}
		
		try
		{
		
			while(true)
				{

					if (keys[keyFire] || keys[119] || keys[keyUp]) System.out.println("UP");
					if (keys[keyRight] || keys[100]) System.out.println("RIGHT");
					if (keys[keyLeft] || keys[97]) System.out.println("LEFT");
					if(keys[keyDown] || keys[115]) System.out.println("DOWN");
					Thread.yield();
				}
			
	

		} 
	catch (Exception e)
		{
				e.printStackTrace();
		}
	}


public boolean handleEvent(Event ev)
	{
		boolean pressed = (ev.id==403 || ev.id==401) ?true:false;
		if(ev.id>=401 && ev.id<=404) keys[ev.key] = pressed;
			return true;
	}

}


That looks identical to what I had before only instead of a Thread, updates are dictated by Timers. (I also used a more complicated handleEvent function seeing as I need to process Mouse events)

Also,

boolean pressed = (ev.id==403 || ev.id==401) ?true:false;

could be

boolean pressed = ev.id==403 || ev.id==401;

?: is unnecessary

well, same bytecode in the end

looks nicer though

@Riven
seems like ?: breaks syntax coloring for true/false.

@OP
Why use a JApplet? It’s best to skip Swing and just use Applet directly. I’ve had focus problems with Swing before so I try to avoid it as much as possible if I’m not using any Swing components.

I can’t exactly remember, but when I just had Applet, my graphics were completely FUBAR’d

Just changed it to Applet. For some reason the uploaded version is flashing a bit. Try it out while I figure out how to stop this madness

took care of the flashing by un-overriding the paint method

Any word on whether it works now or not?

I can confirm that it now works.

~Shazer2 :slight_smile:

I’m confirming keyboard input works for me too now :slight_smile:

What did you change?

AWESOME! ;D

The main change was going from JApplet to Applet. As a consequence, I no longer used paint()/repaint() (the screen would flash black) and instead used getGraphics(). My guess is the former made the most difference. I’m not too sure why swing was the problem; my initial guess might be correct in that the compression screwed with it a little.

I really doubt compression can do anything to the original bytecode that would cause a different execution path. I do agree that it is most likely the usage of Applet over JApplet that fixed the issue :slight_smile: