Presentation is loading. Please wait.

Presentation is loading. Please wait.

SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

Similar presentations


Presentation on theme: "SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved."— Presentation transcript:

1 SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

2

3 Android 3.x ▫First app to use features of Android 3.0+ ▫Using it to animate properties ▫Actually this is an Android 3.1 app AVDs for Android 3.0+ apps are extremely slow ▫Run on an Android device, if possible ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

4 Test driving the app ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

5

6 Technologies Overview Prior to Android 3.0 animation done three ways: ▫Tweened View animations to change View’s appearance, such as where it is displayed, rotation, size the way we did in FlagQuizGame ▫Frame View animations for sequence of images ▫Create animations the way we did it with CannonGame Problem: if you animate a button, can only initiate Button’s click event where the Button was originally ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

7 Technologies Overview Property Animators ▫Used to move and scale ImageViews dynamically ▫Can animate ANY property of ANY object ▫Not limited to Views ▫Can interact with a Button after it has moved Property Animators animate values over time Specify ▫Target object ▫Property to animate ▫Duration of animation ▫Values to animate between for each property ▫How to change the values over time (interpolator) ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

8 Technologies Overview Two property animation classes: Value Animator and Object Animator ValueAnimator ▫Calculates property values over time ▫Must specify AnimatorUpdateListener ▫Programmatically modigy target object’s property values ▫Useful if target object does not have standard set methods for changing property values ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

9 Technologies Overview Two property animation classes ObjectAnimator ▫Subclass of ValueAnimator ▫Uses target object’s set methods to modify object’s animated properties as their values change over time ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

10 New Utility class Added by Android 3.1 ViewPropertyAnimator ▫Simplifies property animation for Views ▫Allows multiple properties to be animated in parallel ▫Now each View contains an animate method  Returns a ViewPropertyAnimator on which you can chain method calls to configure the animation  When last method call on chain finishes, the animation starts  Used to animate spots in this game ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

11 Technologies Overview Two ways to listening for Animation Lifecycle Events Implement interface AnimatorListener ▫Defines methods called when animation starts, ends, repeats or is cancelled Extend class AnimatorListenerAdapter ▫If you do not need all four methods ▫Override only the listener method(s) you need ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

12 Technologies Overview Touch Handling ▫Introduced in CannonGame – overrode Activity method onTouchEvent ▫Two types of touches in the SpotOn game  Touching a spot  Touching elsewhere on the screen Register onClickListeners for each spot (i.e. ImageView) to process touching a spot Use onTouchEvent to process all other screen touches ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

13 Building the App’s GUI Set attributes in string.xml (not shown and not discussed) Set attributes in AndroidManifest.xml file App’s main.xml layout file contains two layouts ▫RelativeLayout  Positions the app’s TextViews for displaying  High score, level and current score ▫LinearLayout (nested inside RelativeLayout)  Display’s lives remaining The layouts and GUI components in this app have all been seen before …and two other XML files ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

14

15

16

17

18 This ImageView will be inflated and configured dynamically for each new spot in the game.

19 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. This ImageView will be inflated and configured dynamically each time a new life is added to the Screen during the game.

20 Building the App Consists of two classes ▫SpotOn – app’s main Activity ▫SpotOnView – subclass of View, it defines the game logic and spot animations ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

21 Construct the SpotOnView Requires 3 arguments: Context in which GUI component is displayed (this Activity) A SharedPreference object The RelativeLayout (so SpotOnView can interact with other GUI components in the layout) layout); Ch5 showed how to read from a SharedPreferences file, here we use default one associated with this Activity

22 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Add the SpotOnView to the RelativeLayout at position 0 – that is, behind all the other elements in the layout Call the SpotOnView’s pause And resume methods

23 OnPause and OnResume onPause releases SoundPool resources and cancels running animations. When an Activity begins executing, ▫The method OnCreate is executed, followed by OnStart, then onResume onResume is also called when an Activity in the background returns to the foreground In this app’s Activity, SpotOnView’s resume method obtains (or re-obtains) SoundPool resources and starts (or restarts) the game This app does not save the game’s state when the app is not on the screen. ©1992-2013 by Pearson Education, Inc. All Rights Reserved.

24 SpotOnView Subclass of View ©1992-2013 by Pearson Education, Inc. All Rights Reserved. OnCreateOnResume OnPause +SpotOnView constructor +pause +resume +cancelAnimations +initializeSoundEffects +resetGame +displayScores +addSpotRunnable +addNewSpot +touchedSpot +missedSpot onTouchEvent +displayScores onClickListener

25 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. For collection of spots and animators

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

27

28

29

30 Call to this from OnCreate view = new SpotOnView (this, getPreferences(Context.MODE_PRIVATE), layout);

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

32

33 For each loops Do not want animations to continue executing when app not on screen

34 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. If dialogDisplayed is true, it is because the end-of-game dialog is still displayed on the screen and user can click the Reset Game button to start over; otherwise, it will call resetGame.

35 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Inflate the life.xml file

36 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Schedule game’s first few spots

37 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Uses CannonGame’s same technique to prepare game’s sound effects

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

39 resetGame’s spotHandler’s postDelayed method receives the addSpotRunnable as an argument. This Runnable’s run method simply calls addNewSpot.

40 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Called -Several times near the beginning of game to display initial spots -Whenever the user touches a spot -When a spot’s animation ends without the spot being touched Set spot’s values

41 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Each new added spot has a listener

42 ©1992-2013 by Pearson Education, Inc. All Rights Reserved. Chained methods Only override two of the four AnimatorListener’s methods

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

44

45

46

47

48

49 Wrap-up Be able to describe three animation mechanisms Know what was added in Android 3.0 and 3.1 that was used in the SpotOn game Know what 4 methods are available in AnimatorListenerAdapter Know how to chain methods to configure an animation Know how to cancel animations Know the methods of the ConcurrentLinkedQueue class and what it is used for ©1992-2013 by Pearson Education, Inc. All Rights Reserved.


Download ppt "SpotOn Game App Android How to Program ©1992-2013 by Pearson Education, Inc. All Rights Reserved."

Similar presentations


Ads by Google