java - Why won't my fishes turn around? -


i'm having trouble moving fishes place , turn around when reach right or left edge. window 600 x 400. in method void swim();, whatever change fish goes same spot (the fish past right edge of window). how can move fish , when reaches right edge or left , turn around , move left edge of window?

note: using gwindow, class provided school.

import java.awt.color; import java.util.random;     import uwcse.graphics.*;  /**  * fish in graphics window  */  public class fish {     public static final int left_moving = 0;     public static final int right_moving = 0;      // graphics window fish belongs     private gwindow window;      // location of fish     private int x;      private int y;      // scale of drawing of fish     private double scale;      private color color;      private int direction;      private oval o1; oval o2;     private triangle tail1; triangle tail2;     private line eye1; line eye2;      /**      * draws fish in graphics window      *       * @param x      *            x coordinate of location of fish      * @param y      *            y coordinate of location of fish      * @param scale      *            scale of drawing of fish      * @param window      *            graphics window fish belongs      */      public fish(int x, int y, double scale,int direction, gwindow window) {         // initialize instance fields         this.x  = x;         this.y = y;         this.scale = scale;         this.window = window;         this.color = randomcolor();         this.direction = direction;          // draw fish         this.draw();     }     private color randomcolor(){         random random = new random();         int r = random.nextint(256);         int g = random.nextint(256);         int b = random.nextint(256);         return new color(r,b,g);                 }             /**      * draws fish      */     private void draw() {          int fsize = (int)(this.scale*15);          this.o1 = new oval(                 this.x - fsize /3,                                                         this.y - fsize /7 ,                                fsize + 4*fsize/3,                                                         fsize  + fsize/5,                                                               this.color,true);                this.tail1 =new triangle(                 this.x - fsize,                                                                               this.y,                                                                          this.x - 2/fsize,                                                                         this.y + fsize/2,                                                                         this.x - fsize,                                                                       this.y + fsize,this.color,true);         this.eye1 = new line(                 this.x + 15*fsize/10,                                                        this.y + 5*fsize/10  ,                                                       this.x + 15 *fsize/10,                                                       this.y + 5* fsize /10,                                                       color.black);            oval o2 = new oval(                 this.x - 37*fsize/5,                                       this.y+ 27*fsize/5 ,                                      fsize + 2*fsize/3,                     fsize  + fsize/5,                                    this.color,true);                line eye2 = new line(                                 this.x - 35*fsize/5,                                  this.y + 30*fsize/5,                                 this.x - 35*fsize/5,                                  this.y + 30*fsize/5, color.black);                               triangle tail2 =new triangle(                 this.x - fsize*5 ,                               this.y + 13*fsize/2,                                  this.x - 10*fsize/2,                                  this.y + 11*fsize/2,                                  this.x - fsize*6,                                 this.y + fsize*6,this.color,true);                   this.window.add(o1);         this.window.add(o2);         this.window.add(tail2);         this.window.add(tail1);         this.window.add(eye1);         this.window.add(eye2);     }       public void moveby (int dx, int dy){         x += dx;         y += dy;             }      public void swim(){         if (right_moving >= 250 || this.direction == right_moving){             this.o1.moveby(3,0);             this.tail1.moveby(3,0);             this.eye1.moveby(3,0);         }     }     }     

you need test fish collision boundaries, , if so, change animation settings change direction of movement. entail using if block or 2 in code move fish. in pseudocode, like:

if fish location equal or less 0     change deltax positive value if fish location + fish width equal or greater width of tank    change deltax negative value   move fish deltax 

i use absolute values of detlax, i.e.,

deltax = math.abs(deltax); 

and

deltax = -math.abs(deltax); 

rather swapping signs. reason if swap signs, risk getting fish stuck @ corner.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -