Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Java by Building Android Games

You're reading from   Learning Java by Building Android Games Learn Java and Android from scratch by building five exciting games

Arrow left icon
Product type Paperback
Published in Mar 2021
Publisher Packt
ISBN-13 9781800565869
Length 686 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John Horton John Horton
Author Profile Icon John Horton
John Horton
Arrow right icon
View More author details
Toc

Table of Contents (24) Chapters Close

Preface 1. Chapter 1: Java, Android, and Game Development 2. Chapter 2: Java – First Contact FREE CHAPTER 3. Chapter 3: Variables, Operators, and Expressions 4. Chapter 4: Structuring Code with Java Methods 5. Chapter 5: The Android Canvas Class – Drawing to the Screen 6. Chapter 6: Repeating Blocks of Code with Loops 7. Chapter 7: Making Decisions with Java If, Else, and Switch 8. Chapter 8: Object-Oriented Programming 9. Chapter 9: The Game Engine, Threads, and the Game Loop 10. Chapter 10: Coding the Bat and Ball 11. Chapter 11: Collisions, Sound Effects, and Supporting Different Versions of Android 12. Chapter 12: Handling Lots of Data with Arrays 13. Chapter 13: Bitmap Graphics and Measuring Time 14. Chapter 14: Java Collections, the Stack, the Heap, and the Garbage Collector 15. Chapter 15: Android Localization – Hola! 16. Chapter 16: Collections and Enumerations 17. Chapter 17: Manipulating Bitmaps and Coding the Snake Class 18. Chapter 18: Introduction to Design Patterns and Much More! 19. Chapter 19: Listening with the Observer Pattern, Multitouch, and Building a Particle System 20. Chapter 20: More Patterns, a Scrolling Background, and Building the Player's Ship 21. Chapter 21: Completing the Scrolling Shooter Game 22. Chapter 22: What Next? 23. Other Books You May Enjoy

Introducing Java methods

Java methods are a way of organizing and compartmentalizing our code. They are quite a complex topic, and a full understanding of them requires knowledge of other Java topics. By the end of the book, you will be a method ninja. However, for now, a basic introduction will be useful.

Methods have names to identify them from other methods and to help the programmer identify what they do. The methods in the Sub' Hunter game will have names such as draw, takeShot, newGame, and printDebuggingText.

Note that code with a specific purpose can be wrapped inside a method; for example, take a look at the following snippet:

void draw(){
      // Handle all the drawing here
}

The preceding method, named draw, could hold all the lines of code that draw our game. When we set out a method with its code, it is called the method definition. The curious-looking prefixed void keyword and the postfixes, (), will be explained in Chapter 4, Structuring Code with Java Methods. However, for now, you just need to know that all of the code inside the draw method will be executed when another part of the code wants it to be executed.

When we want to initiate a method from another part of the code, we say that we call the method. And we would call the draw method with the following code:

draw();

Take note of the following, especially the last point, which is very important:

  • Methods can call other methods.
  • We can call methods as many times as we want.
  • The order in which the method definitions appear in the code file doesn't matter. If the definition exists, it can be called from the code in that file.
  • When the called method has completed its execution, the program execution returns to the line after the method call.

So, in our example program, the flow would look like this:

…
// Going to go to the draw method now
draw(); // All the code in the draw method is executed
// Back from the draw method
// Any more code here executes next
…

Important note

In Chapter 8, Object-Oriented Programming, we will also explore how we can call methods to one file from another file.

By coding the logic of the Sub' Hunter game into methods and calling the appropriate methods from other appropriate methods, we can implement the flow of actions indicated in the flowchart.

Overriding methods

There is one more thing that you need to know about methods before you do some more coding. All the methods I mentioned earlier (for example, draw, takeShot, newGame, and printDebuggingText) are methods that we will be coding. They are our very own methods, and they are for our use only.

Some methods, however, are provided by the Android API and are there for our (and all Android programmers) convenience – we can either ignore them or adapt them. If we decide to adapt them, then this is called overriding.

There are lots of methods that we can override in Android, but one method is overridden so often that it was automatically included in the autogenerated code. Take a look at this part of the code again:

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
}

In the preceding code, we are overriding the onCreate method. Notice that the prefix and postfix to the name are quite complicated. Exactly what is going on here will be explained when we more thoroughly deal with methods in Chapter 4, Structuring Code with Java Methods.

Important note

The super.onCreate… code will also be discussed in depth. But if you can't wait, here is a brief explanation: the super.onCreate… part of the code is calling another version of the onCreate method that also exists, even though we cannot see it. This is the one we are overriding.

Now we can add the method definitions to the Sub' Hunter code.

You have been reading a chapter from
Learning Java by Building Android Games - Third Edition
Published in: Mar 2021
Publisher: Packt
ISBN-13: 9781800565869
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image