java - "this" does not work in my program -
so basically, i'm doing assignment on game, , i'm trying create key commands. thing not work far addkeylistener(**this**)
.i tried fixing ended breaking entire program. code far is:
public static void main (string[] args) throws interruptedexception { (int x = 0 ; x < 999 ; x++) { int lvl = 0; int line = 0; write (); line = start (line); (int p = 0 ; p < 999 ; p++) { if (line == 0) { system.out.println ("select level (1-3)"); lvl = in.getint (); if (lvl == 1) { jframe gamu = new jframe ("hoops 1.0"); //jframe runs game in window culminating_mingxuanhe ball = new culminating_mingxuanhe (); gamu.getcontentpane ().add (ball); gamu.setsize (400, 480); //sets size game gamu.setvisible (true); ball.addkeylistener (this); gamu.setdefaultcloseoperation (jframe.exit_on_close); //closes program when game closed while (true) { ball.moveball (); //runs methods ball.repaint (); thread.sleep (8); //speed of square } } } } if (line == 1) { system.out.println ("goodbye, have nice day"); x = 999; } } } int = 200, s = 1, d = 100, f = 1; //initial position of square int q = 150, w = 430, e = 80, r = 10; int plat = 149; int plat2 = 81; int mov = 10; public void moveball () { if (a + s < 0) //border on left s = 1; if (a + s > getwidth () - 25) //border on right s = -1; if (d + f < 0) //border on top f = 1; if (d + f > getheight () - 25) //border on bottom f = -1; = + s; //moves ball d = d + f; } public void keyboardexample () { keylistener listener = new keypressed (); addkeylistener (listener); setfocusable (true); } public void keypressed (keyevent e) { int key = e.getkeycode (); if (key == keyevent.vk_left) { plat = plat - mov; } if (key == keyevent.vk_right) { plat = plat + mov; } } public void keytyped (keyevent e) { } public void keyreleased (keyevent e) { }
}
you trying access instance of object. can't that, because main method static. static methods part of class, , instance methods part of object. doing in code trying access object used call main (i.e. variable.main), doesn't work because there no object. helped :)
Comments
Post a Comment