Cannon Game

advertisement
Cannon Game
1
Fall 2014 CS7020: Game Design and Development
SurfaceView & SurfaceHolder

SurfaceView:
–

A subclass of view ti which any thread can draw
SurfaceHolder:
SurfaceHolder can manipulate a SurfaceView
– SurfaceHolder can obtain a Canvas on which the user
can draw graphics
– SurfaceHolder provides methods that give a thread
exclusive access to the Canvas for drawing
Each SurfaceView should implement the interface
SurfaceHolder.Callback
–

2
Fall 2014 CS7020: Game Design and Development
SurfaceHolder.Callback Methods

SurfaceChanged:
–

is called when the SurfaceView’s size or orientation
changes, and is typically used to redisplay graphics
based on changes
SurfaceCreated:
is called when the SurfaceView is created, e.g. when
the app first loads or when it resumes from the
background
SurfaceDestroyed
– is called when the SurfaceView is destroyed, e.g.,
when the app terminates
–

2
Fall 2014 CS7020: Game Design and Development
Use Thread to create game loop

Obtain the canvas
–

Only one thread at a time can draw to a
SurfaceView, we must lock the SurfaceHolder
–

Synchronized(SurfaceHolder){……}
Release the canvas
–
2
SurfaceHolder.lockCanvas(null);
SurfaceHolder.unlockCanvasAndPost(canvas
);
Fall 2014 CS7020: Game Design and Development
AlertDialog.Builder

A dialog must be displayed from the GUI main
thread

Anonymous inner class implements Runnable

activity.runOnUiThread(
new Runnable(){
public void run(){
}
}
)
5
Fall 2014 CS7020: Game Design and Development
GestureDetector
6

Complex gesture, such as flings, double-taps, long
presses, and scrolls

Implement GestureDetector.OnGestureListener
and GestureDetector.OnDoubleTapListener
interfaces

GestureDetector.SimpleOnGestureListener is
an adapter class that implements all the methods of
these two interfaces
Fall 2014 CS7020: Game Design and Development
Collision Detection


7
Determine whether the cannonball has
collided with any of the GameView’s
edges, with the blocker or with a section
of the target
We perform simple collision detection
based on the rectangular boundary of the
cannonball
Fall 2014 CS7020: Game Design and Development
Activity Lifecycle Methods
onPause is called when another activity
receives the focus

–
onDestroy is called when an Activity is
shut down.

–
7
suspend game play so that the game does not
continue executing when the user cannot interact
with it
Release the app’s resources
Fall 2014 CS7020: Game Design and Development
Audio Mangement
10

res/raw --- media files

Sound effects are managed with a SoundPool,
which can be used to load, play, and unload sounds

The android documentation recommends that
games use the music audio stream to play sounds

Activity’s setVolumeControlStream specify
that the game’s volume can be controlled with the
device’s volume keys. The method receives a
constant from class AudioManager
Fall 2014 CS7020: Game Design and Development
SoundPool

First argument: the maximum number of
simultaneous sound streams that can play at once

Second argument: which audio stream will be used
to play the sounds

–
There are 7 streams in AudioManager
–
The document recommends
AudioManager.STREAM_MUSIC
Last argument: sound quality.
–
10
The document indicates that this value is not currently
used and 0 should be specified as the default value
Fall 2014 CS7020: Game Design and Development
SoundMap

HashMap --- key, value pairs
–

10
soundMap = new HashMap<Integer, Integer>();
SoundPool.load take three parameters
–
First argument: the application’s context
–
Second argument: resource ID
–
Third argument: the sound’s priority (the argument is
not currently used and should be specified as 1).
Fall 2014 CS7020: Game Design and Development
Download