Natural approach to create a dynamic game engine for Android - java

I want to create a simple game engine for Android. I am fairly new to the OS, and I want to be sure that I do not start off in the wrong direction.
My requirements are fairly simple:
The game engine is an application that runs a game that comes as a bundle of media files (images, audio) + one "description" file (e.g. xml or yaml format) that describes and links different "scenes".
A "scene" is just a background image + a background music + different actions that can be triggered with buttons.
Typically, clicking a button moves the game to a different "scene".
The first question I have is: Should I have one or multiple Activity objects to create the scenes? That is: Is it better to have one single activity which content is dynamically updated when clicking the buttons; or to have one activity per scene.
Note that the difficulty is that the game engine has to generate the activities dynamically. So the list of buttons cannot be hard-coded inside a layout file, as they come from the description file. I have found this example that shows how to create a layout dynamically. Would you recommend using the same approach?
Assuming I use a single activity for all scenes, it means that when I switch scene (by clicking a button), I need to fully update the view (background and buttons). To do that, should I rather remove each element one by one with removeView(), or rather create a new blank layout with setContentView(), and then populate it with the new background and buttons?
Any advice is welcome.
Example of scene:
Background image: A bedroom image
Background music: bedroom_music.mp3
Actions:
Leave the room => Go to Living room scene
Leave the room through the window => Go to Garden scene
Pick up alarm clock => Add clock to inventory

Activities are usually used for different "views". For example:
A chat app will have a lobby activity and starts a new activity when opening a chat screen.
Following the analogy above I would say you should use only one activity and load the correct scene in that activity. There could be an activity controlling these changes? Or an activity that shows a layout while loading.
Having said that. I think you have to design custom objects that can be converted to buttons/walls/monsters or such. This will make the dynamically loading of buttons/objects easier. In Android you must define buttons/visual objects in a layout xml file. This file will be loaded by an activity. I don't think these xml files can be modified on the fly.

Related

How to structure a particular activity (Android)

I am developing an activity that contains a kind of mix between a menu and a progress bar. Below this, there are views that are "visible" or "gone" depending on the progress in which the user is. I have programmed everything in the same activity, but I have doubts that this is efficient because we are talking about more than 1200 lines of code and most of it is only used in part of the progress.
Do you think creating a structure with fragments would be better? If so, how would you go about changing from one fragment to the next from a button within the fragment?

Changing views within an activity

So im wondering how I can change what is displayed on the phone screen without the need for creating a new activity each time I wish to do so.
For example in a simple game im trying to make: there will be a small row of buttons (inventory, stats, save, options, etc). When I press one of these buttons, how can I change the view within the same activity to show the appropriate data without having to create an entirely new task, if possible.
Two possibilities here depending on what you're really trying to accomplish.
1: If this is just another xml layout you want to display I would suggest using fragments.
http://developer.android.com/guide/components/fragments.html
2: If this is graphical ( often true with a game ) you will need to extend SurfaceView and
implement a drawing thread. http://developer.android.com/reference/android/view/SurfaceView.html
( There are multiple examples of how to do this if you google for SurfaceView example ).
It's not a good practice but if it's simple enough you can just use view.SetVisibility(View.Gone) to views you wish to hide and view.SetVisibility(View.Visible) to the views you wish to show.

One layout over another at run time?

My game app has many complex graphical elements and I am worried that having a banner ad continuously on screen will detract too much from the game. My plan is that the user will be able to play each "level" in the game using the full screen without any ads, but upon completion of each level an ad will slide in, on top of the top part of the screen. Presumably this means I have to somehow create a layout programatically that will appear on top of the existing screen layout. Can this be done? If so how?
It can be done.
Afaik you simply have to create a layout which is android:backgroundcolor="transparent".
Else you could do it so you started a new activity on each level completion?

How to implement an activity into another?

I don't know if it's a dumb question because it is one of my first apps.. but if that's the case, please also explain why!
If I create a Tab-Based Activity-Structure, I get nearly what I am asking for but only for Tabs. What I want is generally opening a second or even third activity in one main-activity which contains the main-interface.
Example: I have a Title-Bar and a little icon at the bottom-left corner for some reason during the whole app runtime. Now: How can I control them with the main activity and open at the same time some other activities/views into the existing interface? It should then be shown below the title bar and lying underneath the little icon (the icon is not really important, just fictional). Also it would be nice if I could add some fade in effects to these embedded activities/views. Is that somehow possible?
I currently only know, how to open activities each over another filling the whole screen, except in the case of tabs... maybe I only haven't inspected the tab structure enough.. however, I would be delighted about each answer!
Regards
What you are looking for, are Fragments.
Fragments can be used to fill a part of the screen, while doing something else entirely in a different one.
In your example you can create a main activity that contains two Fragments. One Fragment controls the title bar, the other one controls the main content area.
By replacing the current Fragment in your content area with a different one on the press of a button, you can achieve the behavior you are looking for. At least, that's how I did it in an app of mine containing a main content area and a music player. The music player stays in place while the main content changes.
Sadly I can't provide any example code right now, but here is a tutorialthat should help you get started:
Android User Interface Design: Working With Fragments

Help with code layout

I hope I can explain this properly.
I'm making an android app that, when you open it, it connects to a JSON server, pulls down the data (GPS coords) and dynamically creates a "menu" based on what it received (View1). This will consist of a few buttons, which when clicked, will load a MapView (View2) with the coords gotten from the JSON represented as markers on the map.
I start off with setContentView(R.layout.menu) then get the data, onbuttonClick I load setContentView(R.layout.map) and draw the markers. The problem is, I have an onLocationChangedListener that goes through the code to set up the menu initially. When it tries to build the menu when the mapView is open, I get a force close. Unfortunately, this code also updates the user location and the locations of the overlays and re-draws the map.
My question is: Can I do a check on a layout to say something like if (isActive) so that I can perform actions only if the current view is in focus?
OR should I scrap the whole thing and start again with a better layout? (Suggestions welcome)
Summary::: I have 2 views (menu,map). Need access to same data across both. Currently works with setContentView() but gives me a Force Close when actions are performed on inactive view.
If I understand correctly, you are using setContentView(someLayoutId) to change each time what the Activity is displaying. This is not the way android apps usually work. When you retrieve a resource, you need a root element to reference it, that's why you get the exceptions when the View is not "active".
You have several other options to evaluate:
Create a new MapActivity to show the map
Create a TabActivity and add the map as a new tab
Use a ViewSwitcher to decide whether to show the map or not.

Categories

Resources