Game Development Reference
In-Depth Information
Now, let's work on the main activity and layout classes.
Creating the Game's Activity Class
Listing 3-2 shows the game activity class, ch03.game.sb.SpaceBlaster . This is the core class that will be
executed when the game starts. When an instance of the game is created, the onCreate() method will be
called once. This method loads the user-defined layout described in Listing 3-1.
Listing 3-2. Space Blaster's Main Activity Class
package ch03.game.sb;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class SpaceBlaster extends Activity {
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater factory = LayoutInflater.from(this);
// Set game layout
view = factory.inflate(R.layout.main, null);
setContentView(view);
// Enable view key events
view.setFocusable(true);
view.setFocusableInTouchMode(true);
}
@Override
protected void onStop() {
super.onStop();
((ArcadeGame) view).halt();
}
@Override
protected void onPause() {
super.onPause();
onStop();
}
@Override
protected void onRestart() {
Search WWH ::




Custom Search