tags:

views:

46

answers:

1

I was in the midsts of tinkering in android with the goal of trying to make this small exploration game. I actually got pretty far so far: a nice sprite system, a title screen with a main menu that is all in game code (no UI buttons or anything) that launch various activities. An activity that loads this cool map view, that when clicked on will load up another view of a more detailed "zoomed in" action part of a map. It all works pretty well but now I am left wondering how well I am going about this.

(What happens to a view when you instantiate and then move the set the context to a new view? I am guessing as long as the activity is alive all the instantiations of various views that an activity uses are still good? And in an activities onPause is when id have to save the data thats changed in these views to persist the state in the event the user quits the game etc.. ?)

I have three menu options at the start of the game. The main one is:

New Game

  • The new game, I launch my main map activity that can launch three views: First two are a loading screen for the second, a map screen.

  • Third view is a more detailed action orientated part that uses this sprite engine I developed.

It all works pretty good as in the map view you click on a cell it tells the Calling Activity through a listener to start the detailed action view, and passes in the ID of the cell (so it knows what objects to load in the detailed view based on the cell clicked in second view). (This took me awhile to figure out but someone helped here on another question to get this.) Reference that helped me on this part here. I also used the various information here on ViewSwitcher.

I got even a yes no dialog box in the second view that asks if they really wanna goto that cell when they click on it, a yes tells the calling activity to set the new view. What helped me to get this working was this reference here. I also had to fiddle with the calls to getContext and that, I am still not 100% sure what getResources and getContext do for me.

So is it bad practice to call and load a dialog from a view? If it is, then I don't understand how if I have an event in the view that needs to pop up a dialog how I do that? Call back to the activity and let the activity handle it the dialog response and then call a method on the view for the response?

So far all this works great, I did have a bit to learn about threads too. I spawn a different thread for my MenuView (handles the detection of button clicks and responses), my CellMap view which handles the cool big map that users can click on to see the indepth view which is my SystemView view.

So basically the activity call list is like this:

Home.Activity -> ScrollMap.activity which listens to CellMap wher ethe Yes/No dialog apperas (in the view of CellMap) and if the answer is yes to the question, then the onTouchEvent starts up the SystemView view using


    private CellMap.MapClickedListener onTouchEvent=
        new CellMap.MapClickedListener() {
        @Override
        public void onMapClick(int id) {    
            setContentView(new SolarSystem(theContext,id));                                     
        }
    };

To help anyone else, that listener had to be defined in my CellMap class. Then in the ScrollMap activity when I am about to start the CellMap I just call a method to CellMap sets the map click listener. So far this is the only way I have been able to get data from a dialog (in this case a it was clicked so set the new view) back to the calling activity. Is this proper practice?

Now my biggest question is, I have a playerObject class I want to initialize on new game event (I know how to detect that push) and that then is available to any view in any activity for the life time of the cycle. So far the game activity has just two (3 if you count the loading progress bar) views. I want to get the players name, is that another activity or view? I cannot find a decent tutorial of just how to make a simple input dialogg box that would return a string.

How do i go about passing this stuff around? After I get the players name from wherever that edit box is to be loaded, I want to set the players name in the class and pass that class instance off to another view or activity so they may get at that data. Everything I have searched on this seemed like overkill or not the right way. Whats the best way to pass this "playerClass" around so that I can access it from other views (to say display the players name in every view).

I also have my Home Activity that waits for an onClick on 3 buttons, it then launches their activity that shows the view. I realize a continue game and new game are the same activity and will just change in data loaded off the drive to restore a save game. When the new game button is clicked I would love to get the players name they want to use and of course set the member of the playerClass to this name so it persists.

Where do I call (an edit dialog is it?) the edit dialog from? (more over how do I build one that can take an okay button and input from keyboard etc). Id also like to make it pretty, maybe I could have a simple view that has one edit box in it and a nice image background (though I haven't figured out how to place the edit boxes to fit nicely matching a background i draw for it. I guess id like an edit dialog I can skin to look how i want it to and fit the look of the game).

I am actually kinda happy what I got so far its looking not to bad, just not sure about some specifics like getting user input for a name of the player. Storing that information and passing it then to other activities and views.

Is it better to have one GameMain activity, and a ton of views: Map view Character sheet view inventory view etc etc?

Or Should there be diff activities in there somewhere as well?

+1  A: 

You've written a lot here, so I'm go gonna go ahead and answer the questions I think you're asking.

First, how can one share information between activities and views? If you're just sharing it between views in a single activity, store it in the instance of the Activity subclass.

Sharing between activities requires a little bit more. Instead of using an instance of the default Application class, you can create a subclass of Application and use that instead. Put your data in fields of that subclass. To tell your app to use your subclass, modify the app manifest like so:

<application
    ....
    android:name=".YourAppClassNameHere">
    ....
</application>

As for getting user input, the simple answer is use the built-in EditText view. since you seem to want to give your game a more "custom" style, though, you may need to go about creating your own editable textbox. That and buttons should allow for most sorts of basic user input.

Now, good practice vis-a-vis activities and views. I'm not aware of a standard for this, but when designing I generally try to think of activities as more conceptually separate elements, whereas views are conceptually interwoven. the line between the two is dependent, in part, on the scope of the app; an app with a narrow focus can afford to break more actions down into different activities than can one with a much broader focus.

Your "GameMain" example is a bit on the fence, but I think you've made the right choice: the activity is "playing the game," as opposed to a menu or high-scores table, and the views present different aspects of the game being played.

tlayton
Thanks so much man you guys here are awesome. I did edit the question cause there is a lot there. As I have been hunting around for answers on various things I learned quickly I am not a java OOP pro. So things like listeners, various other call backs inner classes etc are getting me but I am slowly learning and its been great. Your answer helps but what specifically is an activity sublcass? Like I have my GameWorld class that extends Activity ? and in side that I have fields for player name etc etc? Editing question to reflect this answer here...
Codejoy
Also do you have references on creating ones own editable textbox? THe concept of views here confuses me a bit. It seems to me a view can be anything from my games main logic and display to something tiny like a button or a edit box.
Codejoy
Yes, by activity subclass I mean any class which extends Activity. Just like you give it methods (I assume...), you can give it fields which store data.In terms of Android, a view is any class which extends the View class, which is part of the Android SDK. They are used for anything which is meant to be seen by the user, and can be nested within one another to create more complex layouts. If you don't have a lot of experience with Java in general or Android programming in particular, I'd suggest sticking with the EditText view for now, as making one from scratch is a complicated process.
tlayton
Okay final dumb question:<code> public class GameMainActivity extends Activity</code>is what I have, that means im clear to add fields, and I can somehow access these fields from my many views?I have been also looking at androids internal file storage cause it seems to write out a save game this is what I will needhttp://developer.android.com/guide/topics/data/data-storage.html#filesInternalThanks for all the help.
Codejoy
When you are writing the behavior of your game, you are actually putting it (once again, I assume) in methods of GameMainActivity, even if these functions are modifying and being called by various views. That means that the scope of these functions is an instance of GameMainActivity representing the current game, so its fields can be accessed. "Internal storage" is meant more as a place to keep data when your app isn't running (high scores, saved game, etc.) But even this information should be put into a field when the appropriate activity starts, and then saved back to storage when it ends.
tlayton
okay got it, I thought the activity was meant more for a get in and get out. I didnt know its where the meat should go. I can reengineer my software, currently all my stuff like "loadLevel(int x)" etc is done in the view...cause the view then renders the data loaded, handles game physics, and the on Draw to canvas.Seems to me I should build all my game objects (enemies, objects, items etc) in the activity and access them as needed from the view and render them in the view with my sprite class.Thanks for going through this with me, whatever happened to good ol int main()? :)
Codejoy