The first thing we will do is solve the problem of our PongGame
class not being of type View
. Update the class declaration as highlighted, like this:
class PongGame extends SurfaceView {
You will be prompted to import the android.view.SurfaceView
class as shown in the next image:
Click OK to confirm.
SurfaceView
is a descendant of View
and now PongGame
is, by inheritance, also a type of View
. Look at the import
statement that has been added. This relationship is made clear as highlighted next.
android.view.SurfaceView
There are quite a few descendants of View
that we could have extended to fix this initial problem, but we will see as we continue that SurfaceView
has some very specific features that are perfect for games that made this choice the right one for us.
We still have errors in both this class and PongActivity
. Both are due to the lack of a suitable constructor method.
Here is an image showing the error in the PongGame
class since we extended SurfaceView
The error in PongActivity
is more obvious, we are calling a method that doesn't exist. However, the error shown in the previous image is less easily understood.
The PongGame
class, now it is a SurfaceView
must be supplied with a constructor because as mentioned (in a tip) in the previous chapter, once you have provided your own constructor the default (no parameter) one ceases to exist. As the SurfaceView
class implements several different constructors we must specifically implement one or write our own. Hence the previous error.
As none of the SurfaceView
provided constructors are exactly what we need we will provide our own.
If you are wondering how on earth, you know what constructors are supplied and any other details you need to find out about an Android class just Google it. Type the class name followed by API
. Google will almost always supply as the top result, a link to the relevant page on the Android developer's website. Here is a direct link to the SurfaceView
page. https://developer.android.com/reference/android/view/SurfaceView.html. Look under the Public constructors heading and you will see that some constructors are optionally made available.
The PongActivity
also requires us to create a constructor that matches the way we try to initialize it in this line of code.
mPongGame = new PongGame(this, size.x, size.y);
Let's add a constructor that matches the call from PongActivity
that passes in this
and the screen resolution and solve both problems at once.
Remember that PongGame
cannot see the variables in PongActivity
. By using the constructor PongActivity
is providing PongGame
with a reference to itself (this
) as well as the screen size in pixels contained in size.x
and size.y
. Add this constructor to PongGame
. The code must go within the opening and closing curly braces of the class. It is a convention but not required to place constructors above other methods but after member variable declarations.
// The PongGame constructor // Called when this line: // mPongGame = new PongGame(this, size.x, size.y); // is executed from PongActivity public PongGame(Context context, int x, int y) { // Super... calls the parent class // constructor of SurfaceView // provided by Android super(context); }
To import the Context
class, do the following:
Context
in the new constructor's signatureContext
class.Now we have no errors in our bare-bones game engine or the PongActivity
class that initializes it. At this stage, we could run the game and see that using PongGame
as the View
in setContentView
has worked and we have a beautiful blank screen, ready to draw our Pong game. Try this if you like but we will be coding the PongGame
class so that it does something, including adding code to the constructor, next.
18.119.116.102