Creating Splash screen Game Project(Vergine Verses Vampires)
Use the Android SDK Eclipse . To begin working on the V3 game using the SDK,Create a new Android Project. The New > Android Project dialog asks
you for the following information:
1. Project Name: V3
2. Select “Create New Project in Workspace”
3. Use default workspace location (or any place you want)
Figure 2.6 MuseScoreGetting Our Feet Wet: The Splash Screen 27
4. Select “Android 1.6” Build Target
5. Application Name: V3
6. Package Name: com.pearson.lagp.v3
7. Create Activity: StartActivity
8. Min SDK Version: 4
These steps create the project for the example game, including the StartActivity, which for now is a dummy “Hello, World” activity. I’ll fix its content when begin to add code in a later section in this chapter.
Adding the AndEngine Library
If you haven’t already downloaded the AndEngine library from the AndEngine website, do so now. As of this writing, the best place to get the AndEngine .jar file was from the AndEngineExamples repository on Google: http://code.google.com/p/andengineexamples/
Under the source tab, you can either browse for the .jar file (in the lib folder) or clone the entire source tree to your local machine. The latter approach is recommended, as that way you’ll have ready access to the example code. AndEngine uses the Mercurial source code control system. If you don’t already have Mercurial installed,instructions on the website explain how to do so. Once you have the .jar file on your machine, you then need to import the source:
- In the Eclipse Package Explorer, expand the V3 project (if it’s not already expanded). Right-click on the V3 project, and create a lib folder by selecting New Folder and filling in the dialog.
- Figure 1.1 Splash screen for the example game, Virgins Versus Vampires28 Game Elements and Tools Right-click the lib folder and select “Import…” from the pop-up menu.
- Choose General > File System from the next dialog box, and use the Browse button to navigate to the directory where you downloaded the AndEngine .jarfile. Click on the directory name (e.g., …/AndEngineExamples/lib).
- The Import dialog box should now show the directory with an empty check box. The right pane shows the files in the directory, which should include andengine.jar. Click on the check box next to the filename and then click Finish.
- To include the .jar file in your build path, right-click on the .jar file in the Eclipse Project Explorer pane, and then select Build Path > Add to Build Path. The .jar file will now also appear directly under the project folder.
Adding the Splash Screen Code
If i need to modify the automatically generated StartActivity.java file so it displays the splash screen while game components are loaded. Eventually I’ll want to move on to the game menu once everything has loaded, but for now I’ll just leave the splash screen visible until the user returns to the Home screen. Listing shows the modified version of StartActivity.java.
Listing StartActivity.java
import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions. ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy. RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;
public class StartActivity extends BaseGameActivity {
// ===========================================================
// Constants
// ===========================================================
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
// ===========================================================
// Fields
// ===========================================================
private Camera mCamera;
private Texture mTexture;
private TextureRegion mSplashTextureRegion;
// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================
@Override
public Engine onLoadEngine() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}
@Override
public void onLoadResources() {
this.mTexture = new Texture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
this.mSplashTextureRegion = TextureRegionFactory .createFromAsset(this.mTexture,
this, “gfx/Splashscreen.png”, 0, 0);
this.mEngine.getTextureManager().loadTexture(this.mTexture);
}
@Override
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene(1);
/* Center the splash on the camera. */
final int centerX = (CAMERA_WIDTH - this.mSplashTextureRegion.getWidth()) / 2;
final int centerY = (CAMERA_HEIGHT - this.mSplashTextureRegion.getHeight()) / 2;
/* Create the sprite and add it to the scene. */
final Sprite splash = new Sprite(centerX, centerY, this.mSplashTextureRegion);
scene.getLastChild().attachChild(splash);
return scene;
}
@Override
public void onLoadComplete() {
}
}
OutPut SplashScreen Game:
![]() |
| 1.1 Splash screen for the example game, Virgins Versus Vampires |
This may seem like a lot of code to display on one screen, but in AndEngine a splash screen is like any other sprite. Thus what we’ve really done is to set up the initialization of the entire game. Let’s look at the code section by section:
- After the imports are complete, we immediately declare our class to be a subclass of BaseGameActivity. This class performs all of the common AndEngine initialization and provides us with some methods that we’ll override later.
- We then set up the dimensions of the camera viewpoint. Because this is just a splash screen, we want to display the whole scene. The splash screen image, Splashscreen.png, is 480 × 320 pixels in size, corresponding to the default Android display dimensions.
- We next define some fields for the camera, the texture we will use, and the texture region.
- We now override three methods from the superclass BaseGameActivity. Each method corresponds to a point in the loading process:
Three Importent Methods in any Game coding:
- The onLoadEngine() method is called when the game engine is loaded for this activity. We initialize the camera and the engine here. There are two things to note about the engine initialization.First, we set the screen orientation to LANDSCAPE. Most mobile games run in landscape orientation.Second, we asked for a Resolution Policy termed RatioResolutionPolicy. With this policy, the engine will adjust for different screen geometries by scaling features while maintaining the original aspect ratio.
- The onLoadResources() method is called to load required resources. Here we create the Texture we will use and load it from the splash screen image, Splashscreen.png. That file must be found in the folder named assets/gfx in our project workspace, or the application will not run. Don’t worry about the TextureOptions settings for now; drawing and sprites. Finally, we load the texture into the engine’s TextureManager.
- The onLoadScene() method is called when the engine is ready for the scene. We initialize the frames per second logger, create the scene, center the camera on the scene, create the sprite for the splash, attach it to the scene, and return the scene to the engine. AndEngine actually rovides a special type of scene for splash screens (called SplashScene), but the way we’ve handled thematter also gets the job done.
- Right-click on the assets folder under the V3 project in Project Explorer.Select New > Folder, and create the subfolder gfx
- Right-click on the new gfx subfolder and import the file Splashscreen.png. Having the image under the assets folder makes it available for the createFromAsset() method we used to load it into a Texture.
Running the Game in the Emulator
Running the game in the Android Emulator is as simple as choosing Run > Run from the Eclipse menu. If you’re prompted to choose an AVD, do so; you should then see the splash screen displayed in an emulator window. If you need to rotate the emulator to landscape orientation, pressing the key combination Ctrl + F12 will achieve that effect. If the application died with a forced close, you are probably missing one of the needed assets. LogCat will tell you exactly what was not found.
Running the Game on an Android Device
You’ll need to consult your device manufacturer’s developer site to get it properly connected for Android development work. Just make sure that the version of Android running on your device is at least as high as the one you picked when creating the project (in the example, we picked API Level 4, which is equivalent to Android 1.6). Now when you choose Run > Run from the Eclipse menu, you should have the option of running on the device itself. The Android SDK will install the application
on the device and start it running. You can use the Back or Home buttons to return to the home screen, but as with all Android applications, your game will keep running in the background. It’s not actually doing much, because the display is not visible, and Android is smart enough to not spend cycles updating an invisible screen, but the game is still burning some cycles. To actually stop or uninstall the application, use the
Settings > Application > Manage Applications dialog on your device.
