java - KeyPressed not working in each program -
i transferred few programs computer @ school(a mac) @ school home pc. once on computer, noticed keys not work in each program. i've spent hours try figure out why keypressed isn't working. both computers used eclipse
is because of different java version or because of different os? thank you!
example of code:
import java.awt.graphics; import java.awt.image; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.text.decimalformat; import java.util.random; import javax.swing.japplet; public class skeleton extends japplet implements runnable, mousemotionlistener, mouselistener, keylistener { random generator = new random(); boolean gamerunning = true, playagain = true; int width, height; int score = 0; image offscreenimage; int xx; int xy; image cart; image candy[]; int candyx[]; int candyy[]; boolean = false, down = false, left = false, right = false; boolean candyremaining[]; graphics offscr; random r; thread t; boolean inbounds = true; boolean onscreen = true; image title; boolean hasstarted = false; public skeleton() { r = new random(); t = new thread(this); t.start(); } public void init() { xx = 0; xy = 0; candy = new image[3]; candyremaining = new boolean[3]; candyy = new int[3]; candyx = new int[3]; addkeylistener(this); addmousemotionlistener(this); addmouselistener(this); setvisible(true); setsize(500, 500); width = getsize().width; height = getsize().height; title = getimage(getcodebase(), "bkg.png"); offscreenimage = createimage(width, height); // offscr = offscreenimage.getgraphics(); cart = getimage(getcodebase(), "untitled-1.png"); candy[0] = getimage(getcodebase(), "candy1.png"); candy[1] = getimage(getcodebase(), "candy2.png"); candy[2] = getimage(getcodebase(), "candy3.png"); candyremaining[0] = true; candyremaining[1] = true; candyremaining[2] = true; candyx[0] = 100; candyx[1] = 300; candyx[2] = 400; candyy[0] = 425; candyy[1] = 0; candyy[2] = 210; } public void paint(graphics g) { super.paint(g); decimalformat df = new decimalformat("0.00"); random generator = new random(); if (hasstarted) { if (inbounds) { g.drawimage(cart, xx, xy, this); this.candy(g); if (left) { xx -= 5; if (xx < 0) { xx += 5; } } if (right) { xx += 5; if (xx > 500) { xx -= 5; } } if (down) { xy += 5; if (xx > 500) { xy -= 5; } } if (up) { xy -= 5; if (xy < 0) { xy += 5; } } } else { gamerunning = false; } } else { g.drawimage(title, 0, 0, this); } } public void candy(graphics g) { (int count = 0; count < 3; count++) { if (candyremaining[count]) { g.drawimage(candy[count], candyx[count], candyy[count], this); if (candyx[count] < xx + 50 && candyx[count] + 50 > xx) { if (candyy[count] < xy + 50 && candyy[count] + 50 > xy) { system.out.println(count); candyremaining[count] = false; } } } } } public void update(graphics g) { paint(g); } public void run() { while (playagain == true) { while (gamerunning == true) { repaint(); try { thread.sleep(2); } catch (interruptedexception e) { }; } if (gamerunning == false) { } } } public void mousedragged(mouseevent ev) { } public void mousemoved(mouseevent ev) { } public void mouseclicked(mouseevent ev) { if (!hasstarted) { hasstarted = true; } if (gamerunning == false) { if (ev.getx() > 0 && ev.getx() < 1000 && ev.gety() > 0 && ev.gety() < 1000) { gamerunning = true; } } } @override public void mouseentered(mouseevent arg0) { onscreen = true; inbounds = true; } public void mouseexited(mouseevent ev) { onscreen = false; inbounds = false; } @override public void mousepressed(mouseevent arg0) { } @override public void mousereleased(mouseevent arg0) { } @override public void keypressed(keyevent e) { system.out.println("pressed"); int key = e.getkeycode(); if (key == e.vk_a) { left = true; } if (key == e.vk_d) { right = true; } if (key == e.vk_s) { down = true; } if (key == e.vk_w) { = true; } } @override public void keyreleased(keyevent e) { system.out.println("released"); int key = e.getkeycode(); if (key == e.vk_a) { left = false; } if (key == e.vk_d) { right = false; } if (key == e.vk_s) { down = false; } if (key == e.vk_w) { = false; } } @override public void keytyped(keyevent e) { } }
don't use object e
keycodes, use static
keyevent
class. in addition code prettier if use switch
statement instead of if
statements:
switch (e.getkeycode()) { case keyevent.vk_a: // here's change left = true; break;
Comments
Post a Comment