java - What is the proper return for this method? -


this subclass code ship in asteroids remaking making practice.

 package comets;      public class ship extends spaceobject{      protected double angle;     protected double speed;     public ship(double xpos,double ypos,double xvel,double yvel){         super(xpos, ypos, xvel, yvel, 10);     }     public void accelerate(){         this.xvelocity+=.1*math.sin(angle);         this.yvelocity+=.1*math.cos(angle);     }        public double getspeed(){         return speed=math.sqrt(math.pow(this.xvelocity, 2)+math.pow(this.yvelocity,2));     }     public shot fire(){         //need return shot         return null;                 }        public double getangle(){         return angle;            }     public void rotateleft(){         angle+=.1;     }      public void rotateright(){         angle-=.1;     }    } 

as can see not sure how return public shot fire() method. have returning null need return shot ship in game can shoot bullets. bellow shot class have constructor shot defined.

 package comets;  public class shot extends spaceobject{      protected int counter=0;      public shot(double xpos,double ypos,double xvel,double yvel){         super(xpos, ypos, xvel, yvel, 3);     }     public int getage(){         return counter;          }     public void move(){         counter++;         super.move();      }    } 

any appreciated thanks.

because shot not act, acted upon, there should no need shot return anything.

public void fire(){     shot ashot(foo,bar,foobar,foobarbar); } 

the purpose of fire method create instance of shot. because shot originating same location ship, should able query attributes of parent of shot (i assume ship).

i assume shot travels forwards if ship traveling backwards, need angle of ship, , set forward angle of shot.

keep in mind, have multiple instances of shot on screen @ 1 time. in case want create arraylist of shot objects. have class/method (game.loop) iterates on full list of objects, , advances them forward. http://www.tutorialspoint.com/java/java_encapsulation.htm

consider following uml, taking note arrow types may not correct, , constructors not present.

enter image description here

in example loop method contain collision checking, , object advancement. loop check see if player still alive, , check every shot in arraylist see if there collision between player , shot.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -