views:

94

answers:

2

I am trying to determine how to structure the code of a simple android game I am writing. I am focused on the UI portion. The behind the scenes portion (like a pause/restart/switching/etc) does not concern me. The Android demo games source code already do a great job with this.

Essentially my game will need a UI with a series of objects on the screen (roughly 100 or 10x10) that the user can interact with. I need to supply an image for each object as well as implement the touch events and animation events for each object. What I am wondering is what type of class do I use for each object so that I can successfully implement these methods and how do I implement that particular class. Note that this game only executes on a user interaction with one of these objects, meaning I don't need a "real-time" implementation.

My research seems to point in the direction that each screen object needs to be a view object and that I need to place add each view object inside a viewgroup container. I then need to decide on a layout for the viewgroup, but I’m really not sure. Is this a viable approach or are there better alternatives?

I guess I am looking for design methodology more than anything. Thanks for the advice.

+1  A: 

Read up on using SurfaceView, which provides a Canvas onto which you can easily draw objects (give your Objects a draw() method that takes a Canvas as an argument so that they can draw themselves onto it)/Bitmaps/shapes. Look at the sample code for the JetBoy game, that should get you started.

fredley
How do I design the UI to accommodate the fact that I need a way to allow the user to individually interact with each screen object? When I did my first iteration of this project I realized that I was going to have to track the actual location the user touched the screen in order to manipulate each object, and that seemed like a giant pain in the rear vs. just designing the app to have a number of different objects on the screen.
You need to override the various OnTouch events. These will give you the x,y coordinates of the touch point which you can use to update the game state.
fredley
Yep, I follow you there. Perhaps my thought process is trying to avoid having to deal with x, y, coordinates all together. I’m not even sure how I can map an x, y coordinate of a touch event to trigger a specific bitmaps animation on the canvas. The bitmap has a position, but it won’t exactly match the location of the touch event (maybe a for loop comparing to a positional array).
My thought process is if I had a bunch of view objects, an onTouch event could simply trigger that view objects animation and any code processing the game. I can skip dealing with screen location altogether. It is inherent to the view object. Is this wise, or does this make sense?
If you want to do serious graphics you're going to end up using a Canvas/SurfaceView and manually working out which object you've touched. This isn't actually that difficult, especially if your objects are static and/or don't overlap.
fredley
Alright, I guess I will give it a shot seeing how it this project is meant to be a stepping stone. Now, if only I had kept my original code iteration..../sigh
A: 

Each image/object would probably be an extended class of ImageView. Or just an ImageView if it's simple enough.

On your activity code, you can then attach onTouchListeners to these imageview to determine when the use is touching them.

You'll need to use SurfaceView as your container. You can then add your object image views to the surface view and you should be set for a good starting point.

Miguel Morales
Ok. I will start reading up on ImageView and SurfaceView.Thank you.