Ready aim fire
Now, we can give our hero a gun, and later, we can give him enemies to shoot at. We will create a MachineGun
class to do all the work and a Bullet
class to represent the projectiles that it fires. The Player
class will control the MachineGun
class, and the MachineGun
class will control and keep track of all the Bullet
objects that it fires.
Create a new Java class and call it Bullet
. Bullets are not complicated. Ours will need a x and y location, a horizontal velocity, and a direction to help calculate the velocity.
This implies the following simple class, constructor, and a bunch of getters and setters:
public class Bullet { private float x; private float y; private float xVelocity; private int direction; Bullet(float x, float y, int speed, int direction){ this.direction = direction; this.x = x; this.y = y; this.xVelocity = speed * direction; } public int getDirection(){ return direction; } public...