Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cannon Game App Android How to Program

Similar presentations


Presentation on theme: "Cannon Game App Android How to Program"— Presentation transcript:

1 Cannon Game App Android How to Program
© by Pearson Education, Inc. All Rights Reserved.

2 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

3 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.
Test drive the game

4 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

5 1$ indicates first arg, 2$ indicates second arg
© by Pearson Education, Inc. All Rights Reserved. 1$ indicates first arg, 2$ indicates second arg d indicates we are formatting a decimal integer f a floating point value .1f floating point value with one digit after decimal

6 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7 Adding a custom component to a layout’s XML file
© by Pearson Education, Inc. All Rights Reserved. Adding a custom component to a layout’s XML file Must fully qualify the class name in the XML element.

8 Technologies Overview
© by Pearson Education, Inc. All Rights Reserved. String formatting Used with drawText and getString Attaching a Custom View to a Layout Extend class SurfaceView to create CannonView Using the Resource Folder raw Media files placed in res/raw More Activity lifecycle methods onPause called for current activity when another activity receives focus (used to suspend game) onDestroy will release the app’s sound resources

9 Technologies Overview
© by Pearson Education, Inc. All Rights Reserved. Overriding Activity method onTouchEvent For testing which gesture was performed and processing it Using GestureDetector and SimpleOnGestureListener Recognize user actions that represent a series of MotionEvents (flings, double-taps, long presses, scrolls…) Adding Sound with SoundPool and AudioManager SoundPool loads, plays and unloads sounds. Played with an audio stream Uses setVolumeControlStream method

10 Technologies Overview
© by Pearson Education, Inc. All Rights Reserved. Using Threads, SurfaceView and SurfaceHolder for frame-by-frame animation This app performs its animations manually by updating game elements from separate thread of execution Uses subclass Thread with a run method to update positions of game elements, then draw them Use class SurfaceView – a subclass of View to which any thread can draw Games often require complex logic Should be performed in separate threads Those threads need to draw to the screen Manipulated via SurfaceHolder object, which gets a Canvas to draw on

11 Technologies Overview
© by Pearson Education, Inc. All Rights Reserved. Simple Collision Detection Used to determine if cannonball has collided with blocker or with section of target Other more sophisticated frameworks Drawing Graphics using Paint and Canvas Canvas methods : Draw text, lines and circles Canvas draws on View’s Bitmap Paint object specifies drawing characteristics (color, thickness, etc.)

12 Building the App © by Pearson Education, Inc. All Rights Reserved. As mentioned sound files are stored in the app’s res/raw folder. To add them to an app Right click the res folder, select New > Folder Specify folder name raw and click Finish Drag sound files to folder Need to edit the AVDs and add the sound feature May need to first find the .img files for the AVDs in .android/avd folder or just turn off Snapshot enabled In lab, you may actually need to create a temp AVD These app consists of 3 classes Line CannonGame (the activity subclass) CannonView

13 NOTE: Made public so they can be changed, without methods..
© by Pearson Education, Inc. All Rights Reserved. NOTE: Made public so they can be changed, without methods..

14 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

15 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

16 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

17 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

18 SimpleGestureListener is for GestureDetector objects.
© by Pearson Education, Inc. All Rights Reserved. SimpleGestureListener is for GestureDetector objects.

19 UML Diagram View SurfaceView SurfaceHolder.Callback Activity
CannonView CannonView(Context, AttributeSet onSizeChange(…) +newGame() +updatePositions(double) +fireCannonball(MotionEvent) +alignCannon(MotionEvent) +drawGameElements(Canvas) +showGameOverDialog(int) +stopGame surfaceChanged(SurfaceHolder,…) surfaceCreated(SurfaceHolder) surfaceDestroyed(SurfaceHolder) CannonGame onCreate(Bundle) onPause() onDestroy() onTouchEvent(MotionEvent) Thread SimpleGestureListener onDoubleTap(MotionEvent) CannonThread CannonThread(SurfaceHolder) +setRunning run() Line Line() Activity SoundPool AlertDialog Canvas SurfaceHolder

20 Creating a new class in a new file
© by Pearson Education, Inc. All Rights Reserved. Expand the project’s src node in Package Explorer Right click the package (com.deitel.cannongame) and select New > Class to display New Java Class dialog In the Name field, enter CannonView In the Superclass field, enter android.view.View Click Finish Enter java code in the file

21 Packages for sound and drawing
© by Pearson Education, Inc. All Rights Reserved. Packages for sound and drawing

22 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

23 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

24 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

25 Stores reference to parent Activity, so we can display
© by Pearson Education, Inc. All Rights Reserved. In this case, context is the Activity (CannonGame) to which CannonView is attached and attrs contain values set in the layout’s XML document Stores reference to parent Activity, so we can display An AlertDialog in it’s GUI thread

26 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.
The SurfaceHolder method - getHolder() returns the corresponding SurfaceHolder object for managing the SurfaceView SurfaceHolder’s method addCallback stores the object (this) that implements SurfaceHolder.Callback In other words, line 96 registers this (CannonView) as the object that implements SurfaceHolder.Callback to receive method calls that indicate when the SurfaceView is created, updated and destroyed.

27 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

28 Only called once, since this app displays only in portrait mode
© by Pearson Education, Inc. All Rights Reserved. Only called once, since this app displays only in portrait mode Sizes of all game elements dependent on screen size

29 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

30 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

31 The first game completes
© by Pearson Education, Inc. All Rights Reserved. This is true only after The first game completes Start a new game

32 Called by CannonThread’s run method
© by Pearson Education, Inc. All Rights Reserved. Called by CannonThread’s run method New locations calculated based on elapsed time between previous and current frames Simple collision detection, using bounding box

33 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

34 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

35 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

36 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

37 Event has the the double-tap position
© by Pearson Education, Inc. All Rights Reserved. Event has the the double-tap position

38 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

39 Very simple physics, no gravity
© by Pearson Education, Inc. All Rights Reserved. Very simple physics, no gravity

40 Draws on the SurfaceView using the Canvas that the
CannonThread obtains from the SurfaceView’s SurfaceHolder © by Pearson Education, Inc. All Rights Reserved.

41 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

42 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

43 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

44 A dialog must be displayed from the GUI thread,
© by Pearson Education, Inc. All Rights Reserved. A dialog must be displayed from the GUI thread, so Activity’s method runOnUiThread is called and passed an object of an anonymous inner class that Implements Runnable.

45 Called by CannonGame’s onPause method
© by Pearson Education, Inc. All Rights Reserved. Called by CannonGame’s onPause method Called by CannonGame’s onDestroy method

46 Required to implement Interface SurfaceHolder.Callback
© by Pearson Education, Inc. All Rights Reserved. Required to implement Interface SurfaceHolder.Callback Empty, because app always in portrait mode or resumed

47 Wait until thread has stopped
© by Pearson Education, Inc. All Rights Reserved. Request to stop thread Wait until thread has stopped

48 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

49 The Game Loop Only one thread at a time can draw to
© by Pearson Education, Inc. All Rights Reserved. Only one thread at a time can draw to A SurfaceView, so we must lock it Ensures game operates at same speed regardless of how fast device is (if device is faster, game elements will move less and vice versa)

50 ©1992-2013 by Pearson Education, Inc. All Rights Reserved.


Download ppt "Cannon Game App Android How to Program"

Similar presentations


Ads by Google