Welcome into part 2 of the Android blog series !
Today, I will try to explain the concepts I discovered when creating the project.
Finding a Sample:
When trying new libraries or new functionalities, I always try to find sample code I can analyse and copy to my own project. Fortunately, Android project pages contains such samples.
One of them seems to be a perfect starting point for my game: The Lunar Lander Sample. It’s a classic game where you want to land a lunar module by switching off and on reactors. The difficulty is that the module follows gravity rules, and you really need to predict it’s behavior.
Remember this game now ?
The documentation explains to a great extend the source code of the sample, so I won’t repeat it here. Well, just in two words then.
General Guidelines:
The Main class is called an Activity. An Activity is like a controler (in MVC pattern), there can be many activities in a single application. Think of it has a screen in your application. It creates a View class, either programmatically or by reading an XML description file. The View class represents, errr…. the View part of the MVC pattern.
All images, view xml files and string resources are parsed by a sdk tool that generates a class, the R class (what’s this name ???), that contains the Ids of these resources. You can write your application like that:
- You design you application U.I. using the XML file (and the plugin shows you instantly the result).
- You override the onCreate function in your activity class to load and set the view to display.
- From the View class you just created, you get the Java classes of your controls (buttons, input fields) by using their Ids in the generated R.class
- You then can get / set the values of these controls, listen to a click, etc….
- Of course, for a game, you don’t use the View classes hierarchy, but directly draw into a canvas. More on that later.
NB: The eclipse plugin handles SDK tools for you. It re-generates automatically the R.class (Did I told you the name was awful ?) whenever you create a new resource, it immediatly shows a bad reference in your layout file, it can display the result of your layout (screen below), and it packs the project and installs it into the emulator when you click on the Eclipse Run button.

GC.

Commentaires
Ecrire un commentaire Trackback