Objectives
In this chapter you’ll:
Create a simple game app that’s easy to code and fun to play.
Create a custom SurfaceView
subclass for displaying the game’s graphics from a separate thread of execution.
Draw graphics using Paint
s and a Canvas
.
Override View
’s onTouchEvent
method to fire a cannonball when the user touches the screen.
Perform simple collision detection.
Add sound to your app using a SoundPool
and the AudioManager
.
Override Fragment
lifecycle methods onPause
and onDestroy
.
6.2 Test-Driving the Cannon Game app
6.3.1 Attaching a Custom View
to a Layout
6.3.2 Using the Resource Folder raw
6.3.3 Activity
and Fragment
Lifecycle Methods
6.3.4 Overriding View
Method onTouchEvent
6.3.5 Adding Sound with SoundPool
and AudioManager
6.3.6 Frame-by-Frame Animation with Thread
s, SurfaceView
and SurfaceHolder
6.3.7 Simple Collision Detection
6.3.8 Drawing Graphics Using Paint
and Canvas
6.4 Building the App’s GUI and Resource Files
6.4.5 Adding the Sounds to the App
6.5 Class Line
Maintains a Line’s Endpoints
6.6 MainActivity
Subclass of Activity
6.7 CannonGameFragment
Subclass of Fragment
6.8 CannonView
Subclass of View
6.8.1 package
and import
Statements
6.8.2 Instance Variables and Constants
6.8.4 Overriding View
Method onSizeChanged
6.8.10 Method showGameOverDialog
6.8.11 Methods stopGame
and releaseResources
6.8.12 Implementing the SurfaceHolder.Callback
Methods
6.8.13 Overriding View Method onTouchEvent
6.8.14 CannonThread
: Using a Thread
to Create a Game Loop
Self-Review Exercises | Answers to Self-Review Exercises | Exercises