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

Structuring Sub' Hunter with methods

As we add the method definitions to the code, it should come as no surprise where each of the methods will go. The draw method will go after the comment about do all the drawing… and so on.

Add the newGame method definition after the appropriate comment, as shown here:

/*
     This code will execute when a new
     game needs to be started. It will
     happen when the app is first started
     and after the player wins a game.
 */
void newGame(){
}

Add the draw method definition after the appropriate comment, as highlighted here:

/*
     Here we will do all the drawing.
     The grid lines, the HUD,
     the touch indicator and the
     "BOOM" when a sub' is hit
*/
void draw() {
}

Add the onTouchEvent definition after this comment, as follows:

/*
     This part of the code will
     handle detecting that the player
     has tapped the screen
 */
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
}

Note that the onTouchEvent method is another overridden method. Android provides this method for our benefit, and when the player touches the screen, it will call this method. All we need to do now is work out how to handle a touch when the onTouchEvent method gets called. There is also an error in this code, but we will resolve this when we begin learning about OOP later.

Now, add the takeShot method definition after the comment, as follows:

/*
     The code here will execute when
     the player taps the screen It will
     calculate the distance from the sub'
     and determine a hit or miss
 */
void takeShot(){
}

Add the boom method definition after the comment, as follows:

// This code says "BOOM!"
void boom(){
}

Now, add the printDebuggingText definition after the comment about the debugging text:

// This code prints the debugging text
void printDebuggingText(){
}

As the project progresses, we will add code to each of the method definitions because, at the moment, they are empty and, therefore, don't do anything. Furthermore, as we learn more about methods, the postfixes and prefixes of the method names will also evolve and become easier to understand.

A concept that is very closely related to methods and useful for understanding them better is OOP.

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