Game Development Reference
In-Depth Information
Scene Player
The scene player is where the game prototype comes to life. This is the screen where the objects' scripts run and all
the action takes place. The user interface is presented in a similar fashion to that of the scene editor: an empty area
represents the scene, and the GUI area consists of a play/pause button and a stop button. Pressing the play button
runs the GPTJNILib class's native Update function in a loop and swaps the button with a pause button. Pressing the
Pause button causes the application to skip updates, which keeps every game object in its current position. The Stop
button resets the scene, bringing all the game objects back to their initial state. Finally, pressing the Back button on
the device closes the scene player and shows the scene editor.
The first thing you need for playing is a list of game objects that will be kept up to date. Then you'll need a
variable to tell if the simulation is playing, as opposed to being paused or stopped. You'll also need a variable that
holds the last frame's timestamp, which is used to compute delta time. The last two member variables in the class,
OBJECT_RADIUS and GUI_SCREEN_HEIGHT_PCT , determine the size of each game object and the height of the simulation
control buttons, respectively. See Listing 12-5 for the code.
Listing 12-5. GPTScenePlayerView Member Variables, Constructor, and Example of OnBackPressed Function
private ArrayList<GPTGameObject> mUpdatedGameObjects;
private boolean mIsPlaying;
private long mLastTime;
private static final float OBJECT_RADIUS = 20.0f;
private static final float GUI_SCREEN_HEIGHT_PCT = 0.125f;
public GPTScenePlayerView(Context context) {
super(context);
mIsPlaying = false;
mUpdatedGameObjects = newArrayList<GPTGameObject>();
}
public void onBackPressed(GPTActivity activity) {
activity.SetCurrentView(GPTActivity.sceneEditorView);
}
There is nothing special about the constructor or the OnBackPressed function. The first just calls the base
constructor and sets some default values, while the second is a handler function that gets called by GPTActivity
whenever the user presses the back key on their device. In this particular case, pressing this key on the scene player
takes the user back to the scene editor.
SetSceneObjects is the first major function of the scene player class. It takes a list of GPTEditorGameObject
instances and populates an internal list of GPTGameObject instances, losing the String-typed script property in
the process. It also turns the list of editor objects into an array and passes the array to native code via GPTJNILib 's
Initialize function as shown in Listing 12-6.
Listing 12-6. GPTScenePlayerView SetSceneObjects Function
public void SetSceneObjects(
ArrayList<GPTEditorGameObject> sceneObjects)
{
mUpdatedGameObjects.clear();
for (GPTEditorGameObject obj : sceneObjects)
{
mUpdatedGameObjects.add(new GPTGameObject(
obj.id, obj.x, obj.y, obj.color));
}
 
Search WWH ::




Custom Search