java - Rectangle is not moving if i set the x y from another class. -


alright, got 2 classes here, main , keylistener class use pass keyboard inputs main class.

    import java.awt.canvas; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.point; import java.awt.image.bufferstrategy; import java.util.arraylist; import java.util.random;  import javax.swing.jframe;  public class game extends canvas implements runnable{      private static final long serialversionuid = 1l;      private final static int width = 240;     private final static int height = width / 12 * 9;     private final static int scale = 2;     private string title = "game";      private boolean running = false;      private thread thread;      random r = new random();      arraylist<point> snakepart = new arraylist<point>();     point head;      keyhandle keyhandle;        public static void main(string[] args) {         new game();     }      public game() {         init();          jframe frame = new jframe(title);         dimension size = new dimension(width * scale, height * scale);          setminimumsize(size);         setmaximumsize(size);         setpreferredsize(size);          frame.setresizable(false);         frame.setlocationrelativeto(null);         frame.setdefaultcloseoperation(jframe.exit_on_close);          frame.add(this);         frame.pack();          frame.setvisible(true);            start();      }      public void init() {         head = new point(0,0);         addkeylistener(new keyhandle());     }      @override     public void run() {          final double ticks = 60.0;         long inittime = system.nanotime();         double ns = 1000000000 / ticks;         double delta = 0;          long timer = system.currenttimemillis();         double updates = 0;         int frames = 0;          while (running) {             long nowtime = system.nanotime();             delta += (nowtime - inittime) / ns;             inittime = nowtime;             if (delta >= 1) {                 tick();                 updates++;                 delta--;              }              render();             frames++;              if (system.currenttimemillis() - timer > 1000) {                 timer += 1000;                 system.out.println("ticks: " + updates + " frames: " + frames);                 updates = 0;                 frames = 0;             }         }          stop();      }        public void tick() {        }        public void render() {          bufferstrategy bs = getbufferstrategy();         if (bs == null) {             createbufferstrategy(3);             return;         }          graphics g = bs.getdrawgraphics();          g.setcolor(color.black);         g.fillrect(0, 0, getwidth(), getheight());          g.setcolor(color.white);         g.drawrect(head.x, head.y, 10, 10);            bs.show();         g.dispose();      }      private synchronized void start() {         if (running)             return;          running = true;         thread = new thread(this);         thread.start();     }      private synchronized void stop() {         if (!running)             return;          running = false;          try {             thread.join();         } catch (interruptedexception e) {             // todo auto-generated catch block             e.printstacktrace();         }          system.exit(1);     }   } 

and i've got keylistener class looks this

import java.awt.event.keyevent; import java.awt.event.keylistener;  public class keyhandle implements keylistener {      game game;      @override     public void keypressed(keyevent e) {         if(e.getkeycode() == keyevent.vk_up){           } else if(e.getkeycode() == keyevent.vk_down){             game.head.y += 10;           } else if(e.getkeycode() == keyevent.vk_left){          } else if(e.getkeycode() == keyevent.vk_right){          } else if(e.getkeycode() == keyevent.vk_escape){          }       }      @override     public void keyreleased(keyevent e) {         if(e.getkeycode() == keyevent.vk_up){          } else if(e.getkeycode() == keyevent.vk_down){          } else if(e.getkeycode() == keyevent.vk_left){          } else if(e.getkeycode() == keyevent.vk_right){          } else if(e.getkeycode() == keyevent.vk_escape){          }       }      @override     public void keytyped(keyevent e) {         // todo auto-generated method stub      }    } 

now problem appears when try edit "head" y , x values seen in second class.

if(e.getkeycode() == keyevent.vk_down){             game.head.y += 10; 

what doing wrong here , there better way pass , set x , y values more efficiently.

your keyhandle class contains reference game no intantiation of game. instead of creating new game() in main method , dropping instance on floor, allocate instance reference contained in keyhandle.

keyhandle.game = new game();


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -