Coding the Bullet class
The Bullet
class is not complicated. It is a tiny square bouncing around the screen and will have many similarities to the ball in the Pong game. To get started, add the member variables shown highlighted next.
import android.graphics.RectF; class Bullet { // A RectF to represent the size and location of the bullet private RectF mRect; // How fast is the bullet traveling? private float mXVelocity; private float mYVelocity; // How big is a bullet private float mWidth; private float mHeight; }
We now have a RectF
called mRect
that will represent the position of the bullet. You will need to add the import statement for the RectF
class as well. In addition, we have two float
variables to represent the direction/speed of travel (mXVelocity
and mYVelocity
) and two float variables to represent the width and height of a bullet (mWidth
and mHeight
).
Next, add the Bullet
constructor that can be called when a new Bullet
object is created and...