Learning Java by Building Android  Games
上QQ阅读APP看书,第一时间看更新

Revisiting the code and methods we have used already

At this point, it might be worth revisiting some of the code we have seen in the book so far where we settled for a cursory explanation. We are now able to understand everything we have written. However, don't concern yourself if there are still a few areas of confusion as the more you use this OOP stuff the more it will sink into your mind. I imagine if you continue with this book and game coding that over the next year or two you will have multiple "Oh!! Now I get it" moments when OOP and Android concepts finally click into place.

To help these moments arrive let's revisit some old code and take a very quick look at what is happening.

In the onCreate method, we saw this signature

protected void onCreate(Bundle savedInstanceState) {

The method receives an object of type Bundle named savedInstanceState. The Bundle class is a big deal in GUI based Android apps that often use more than one Activity. However, we will only need one Activity for game projects and we won't need to explore the Bundle class any further in this book.

In the onTouchEvent method we saw this signature.

public boolean onTouchEvent(MotionEvent motionEvent) {

The previous signature indicates that the method returns a boolean and receives an object called motionEvent which is of the type MotionEvent. We will explore the MotionEvent class in Chapter 7, Making Decisions with Java If, Else and Switch and we will fully reveal classes and objects in Chapter 8, Object-Oriented Programming.

This next line is the method's line of code which returns a boolean, as required.

return true;

Let's spawn the enemy sub'.