eclipse - Object (Sprite) in 2D java game moves abnormally -


my sprite in test game flies off screen @ super high rates when pressing key inputs have , don't know why.

keyinput.java:

package com.cosmicluck.miner.framework;  import java.awt.event.keyadapter; import java.awt.event.keyevent;  import com.cosmicluck.miner.window.handler;  public class keyinput extends keyadapter {     handler handler;      public keyinput(handler handler)     {         this.handler = handler;     }      public void keypressed(keyevent e)     {         int key = e.getkeycode();          for(int = 0; < handler.object.size(); i++)         {             gameobject tempobject = handler.object.get(i);              if(tempobject.getid() == objectid.player)             {                 if(key == keyevent.vk_d) tempobject.setvelx(5);                 if(key == keyevent.vk_a) tempobject.setvelx(-5);             }         }          if(key == keyevent.vk_escape)         {             system.exit(1);         }     }     public void keyreleased(keyevent e)     {         int key = e.getkeycode();          for(int = 0; < handler.object.size(); i++)         {             gameobject tempobject = handler.object.get(i);              if(tempobject.getid() == objectid.player)             {                 if(key == keyevent.vk_d) tempobject.setvelx(0);                 if(key == keyevent.vk_a) tempobject.setvelx(0);             }         }     } } 

changing velocity 0 keypressed make object fly off screen.

and here player.java:

package com.cosmicluck.miner.objects;  import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.rectangle; import java.util.linkedlist;  import com.cosmicluc.miner.framework.gameobject; import com.cosmicluc.miner.framework.objectid;  public class player extends gameobject {  private float width = 48, height = 96;  private float gravity = 0.5f; private final float max_speed = 10;  public player(float x, float y, objectid id) {     super(x, y, id); }  public void tick(linkedlist<gameobject> object) {      x += velx;     y += vely;     if(falling || jumping)     {         vely += gravity;          if(vely > max_speed)             vely = max_speed;     } }  public void render(graphics g)  {     g.setcolor(color.blue);     g.fillrect((int)x, (int)y, (int)width, (int)height);      graphics2d g2d = (graphics2d) g;     g.setcolor(color.red);     g2d.draw(getbounds());     g2d.draw(getboundsright());     g2d.draw(getboundsleft());     g2d.draw(getboundstop()); }  public rectangle getbounds() {     return new rectangle((int) ((int)x+(width/2)-((width/2)/2)), (int) ((int)y+(height/2)), (int)width/2, (int)height/2); } public rectangle getboundstop() {     return new rectangle((int) ((int)x+(width/2)-((width/2)/2)), (int)y, (int)width/2, (int)height/2); } public rectangle getboundsright() {     return new rectangle((int) ((int)x+width-5), (int)y+5, (int)5, (int)height-10); } public rectangle getboundsleft() {     return new rectangle((int)x, (int)y+5, (int)5, (int)height-10); } 

}

gameobject.java:

package com.cosmicluck.miner.framework;  import java.awt.graphics; import java.awt.rectangle; import java.util.linkedlist;  public abstract class gameobject  {  protected float x, y; protected objectid id; protected float velx = 0, vely = 0; protected boolean falling = true; protected boolean jumping = false;  public gameobject(float x, float y, objectid id) {     this.x = x;     this.y = y;     this.id = id; }  public abstract void tick(linkedlist<gameobject> object); public abstract void render(graphics g); public abstract rectangle getbounds();  public float getx() {     return x; } public float gety() {     return y; } public void setx(float x) {     this.x = x; } public void sety(float y) {     this.y = y; }  public float getvelx() {     return velx; } public float getvely() {     return vely; } public void setvelx(float velx) {     this.velx = x; } public void setvely(float vely) {     this.vely = y; }  public boolean isfalling() {     return falling; }  public void setfalling(boolean falling) {     this.falling = falling; }  public boolean isjumping() {     return jumping; }  public void setjumping(boolean jumping) {     this.jumping = jumping; }  public objectid getid() {     return id; } 

}

game.java:

package com.cosmicluck.miner.window;  import java.awt.canvas; import java.awt.color; import java.awt.graphics; import java.awt.image.bufferstrategy; import java.util.random;  import com.cosmicluc.miner.framework.keyinput; import com.cosmicluc.miner.framework.objectid; import com.cosmicluc.miner.objects.player;  public class game extends canvas implements runnable { private static final long serialversionuid = -905290418752159869l;  private boolean running = false; private thread thread;  public static int width, height;  //object handler handler;  random rand = new random();  private void init() {     width = getwidth();     height = getheight();     handler = new handler();      handler.addobject(new player(100, 100, handler, objectid.player));      handler.createlevel();      this.addkeylistener(new keyinput(handler)); }  public synchronized void start(){     if(running)         return;      running = true;     thread = new thread(this);     thread.start(); }  public void run() {     init();     this.requestfocus();     long lasttime = system.nanotime();     double amountofticks = 60.0;     double ns = 1000000000 / amountofticks;     double delta = 0;     long timer = system.currenttimemillis();     int updates = 0;     int frames = 0;     while(running){         long = system.nanotime();         delta += (now - lasttime) / ns;         lasttime = now;         while(delta >= 1){             tick();             updates++;             delta--;         }         render();         frames++;          if(system.currenttimemillis() - timer > 1000){             timer += 1000;             system.out.println("fps: " + frames + " ticks: " + updates);             frames = 0;             updates = 0;         }     } }  private void tick() {     handler.tick(); } private void render() {     bufferstrategy bs = this.getbufferstrategy();     if(bs == null)     {         this.createbufferstrategy(3);         return;     }      graphics g = bs.getdrawgraphics();     ////////////////////////////////////     //draw here     g.setcolor(color.black);     g.fillrect(0, 0, getwidth(), getheight());      handler.render(g);      ////////////////////////////////////     g.dispose();     bs.show(); }  public static void main(string args[]) {     new window(800, 600, "miner madness - prototype", new game()); } 

}

thank ahead of time!

edit: fixed! had silly error in gameobject when making velx/vely = x,y respectively. thank help! :)

you should assign method parameters fields, not other fields.

public void setvelx(float velx) {     this.velx = velx; } public void setvely(float vely) {     this.vely = vely; } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -