HOw to move my bullet slowly in java? -


hi developing game fighter moves right , left , shoots. shooting part, tried use loop slow speed down , user can see bullet. wasn't enough. used sleep not answer. have no idea do. here paintcomponent calss:

package game;  import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.awt.geom.rectangle2d; import java.awt.image.bufferedimage; import java.io.file; import java.io.ioexception; import java.util.arraylist; import javax.imageio.imageio; import javax.swing.jpanel;    public class paintcomponent extends jpanel implements keylistener {     int dx = 200-15;     int dy = 450;     int = 450;      arraylist<bullet> bullets = new arraylist<>();     public rectangle2d rec =new rectangle2d.double(dx , dy, 30, 10);     rectangle2d recb = new rectangle2d.double(dx+13 , my, 6, 6);  //    public polygon pol = new polygon     private bufferedimage imagebg, imagefi, imagebu;       public paintcomponent() {         this.addkeylistener(this);         this.setfocusable(true);         this.setbackground(color.white);          try {                           imagebg = imageio.read(new file("c:\\java\\netbeansprojects\\game\\bg.jpg"));           imagebu = imageio.read(new file("c:\\java\\netbeansprojects\\game\\bul.png"));           imagefi = imageio.read(new file("c:\\java\\netbeansprojects\\game\\fi.png"));        } catch (ioexception ex) {             system.out.println("no background image available!");        }     }      public void shoot(){         if(bullets != null){             for(int i=0; i<bullets.size(); i++){                 for(int j=0; j<200; j++){                     bullets.get(i).setdy(my-j);                 }                 system.out.println(bullets.get(i).getdy());             }         }     }     public void moverec(keyevent evt){         switch(evt.getkeycode()){             case keyevent.vk_left:             dx -= 5;             rec.setrect(dx, dy, 30, 10);             recb.setrect(dx+13, dy, 6, 6);             repaint();             break;         case keyevent.vk_right:             dx += 5;             rec.setrect(dx, dy, 30, 10);             recb.setrect(dx+13, dy, 6, 6);             repaint();             break;         case keyevent.vk_space:             bullet b = new bullet(dx, dy);             bullets.add(b);             shoot();             break;      } }          @override         public void paintcomponent(graphics grphcs)             {super.paintcomponent(grphcs);             graphics2d gr = (graphics2d) grphcs;                 int x = (int) rec.getcenterx();                 int y = (int) rec.getcentery();                 gr.drawimage(imagebg, 0, 0, null);                 gr.drawimage(imagefi, x-50, y-75, null);                 gr.setcolor(color.gray);                 if(bullets != null){                     for(int i=0;i<bullets.size();i++){                    gr.drawimage(imagebu, null, bullets.get(i).getdx(), bullets.get(i).getdy());                    repaint();                     }                 }                 gr.draw(recb);             }      @override     public void keytyped(keyevent e) {     }     @override     public void keypressed(keyevent e)      {moverec(e);}     @override     public void keyreleased(keyevent e)      {} } 

and bullet calss:

package game;  public class bullet {     private int x, y, newy;      public bullet(int x, int y){         this.x = x;         this.y = y;     }      public int getdy(){         return y;     }     public void setdy(int newy){         y = newy;     }     public int getdx(){         return x;     } } 

i think going wrong slowing down loop. last thing want slow down game loop or sleep game loop. affect objects in game.

there multiple way go this:

smaller increments per tick

one of obvious things make increment of bullet smaller. lets take @ shoot(); method:

  public void shoot(){     if(bullets != null){         for(int i=0; i<bullets.size(); i++){             for(int j=0; j<200; j++){                 bullets.get(i).setdy(my-j);             }             system.out.println(bullets.get(i).getdy());         }     } } 

as far understand iterating 200x on bullets, each tick bullet's y axis gets changed, using formula "my - j" or "450 - tick index"

in order slow down bullet need divide j number desired speed of bullet. instance: "my - (j / 2)" impact speed of bullet. try play around these variables desired speed.

adding speed modifier

what lot of games speed modifier or base speed each projectile. of use you, thing noticed kinda trying simulate loss of velocity. achieve result need variable. let call "time live" right now.

so if modify bullet class this. noticed have new function called move();. calculate next move based upon variables.

public class bullet { private int x, y, newy; private speed, ttl; //ttl = time live  public bullet(int x, int y, int speed){     this.x = x;     this.y = y;     this.speed= speed;     this.ttl = 250; }  public int move() {     //do calculation perform loss of velocity within reasonable range. because these number might overkill     this.speed -= (ttl / 100);     y += this.speed;     ttl--; }  public int getdy(){     return y; } public void setdy(int newy){     y = newy; } public int getdx(){     return x; } } 

what code calculates speed based upon time live variable longer bullet live less velocity have. adjusting speed variable makes able control bullet better. , myself looks lot more neater in shoot method:

public void shoot(){     if(bullets != null){         for(int i=0; i<bullets.size(); i++){             bullets.get(i).move();         }     } } 

of course there more it, checking if speed , time live dont go out of bounds , stuff, think smart enough figure out ;)

running off timer

as controlaltdel said can implement timer, im not expert on java im not going in depth on this. surely possibility. nothing more implement current shoot method inside tick function of timer. of course removing i<200 loop. since not effecient.

anyways

if did wrong or misunderstood (or grammer mistaked :) ) problem, im sorry. if there question loved hear ;).

and please not untested code , im here explain things try working intended!

sincerly,

syntasu.

update:

some explaining on how update bullet's. in order update bullets need make run off loop. since in case main loop drawing happening, "paintcomponent" method. there loop withing paint component draw bullet, thing have add our logic regarding .move(); method.

the paint component following ( + fixed tabbing ):

@override public void paintcomponent(graphics grphcs) {     super.paintcomponent(grphcs);      graphics2d gr = (graphics2d) grphcs;     int x = (int) rec.getcenterx();     int y = (int) rec.getcentery();      gr.drawimage(imagebg, 0, 0, null);     gr.drawimage(imagefi, x-50, y-75, null);     gr.setcolor(color.gray);      if(bullets != null)     {         for(int i=0;i<bullets.size();i++)         {             //here loop on bullet list, lets add move method             bullets.get(i).move();              gr.drawimage(imagebu, null, bullets.get(i).getdx(), bullets.get(i).getdy());             repaint();         }     }      gr.draw(recb); } 

the thing added "bullets.get(i).move();". run every frame. work in theory (inb4 im not testing these codes). going assumption use multiple instance's of bullet class, each class should encapsulate own speed , time live variable.

implementing make shoot method obsolete. can move code inside paintcomponent related shooting , move shoot function.

regarding time live variable, add 1 more piece code. take care of garbage collection of bullets. since live indefinitly:

for(int i=0;i<bullets.size();i++) {     bullet b = bullets.get(i);     if(b.ttl >= 1)     {         bullets.get(i).move();         gr.drawimage(imagebu, null, b.getdx(), b.getdy());     }     else     {         //remove bullet list         //in c# bullets.remove(b);     }      repaint(); } 

hopefully resolves issue of bullet not moving. , potential performance issue due bullets not being destroyed. in before, there questions love hear them! ;)


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -