* GameActivity Class Setup
Now we are going to create the base classes for our game which are responsible for handling various operations like resource management, scene management etc. So lets start with this.
Step 1: Expand you project > Src folder > Right click on Package name > New > Class
Name the class as 'GameActivity' and click finish.
Now extends the class as BaseGameActivity, now eclipse should give error to add unimplemented methods, hover your mouse on class name and click on Add unimplemented methods.
While creating scenes, we will have to add an unimplemented method called onBackKeyPressed() - We will take care of executing it inside game activity, override the onKeyDown method.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
}
return false;
}
By doing that, the code responsible for handling actions executed after pressing the phone`s back button.
Now we are going to create the base classes for our game which are responsible for handling various operations like resource management, scene management etc. So lets start with this.
Step 1: Expand you project > Src folder > Right click on Package name > New > Class
Name the class as 'GameActivity' and click finish.
Now extends the class as BaseGameActivity, now eclipse should give error to add unimplemented methods, hover your mouse on class name and click on Add unimplemented methods.
package com.mindew.fruitecollector; import java.io.IOException; import org.andengine.engine.options.EngineOptions; import org.andengine.entity.scene.Scene; import org.andengine.ui.activity.BaseGameActivity; public class GameActivity extends BaseGameActivity { public EngineOptions onCreateEngineOptions() { return null; } public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws IOException { } public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException { } public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException { }}
The next step is to create our Engine, To make our game work at similar speeds on veriety of devices, we will have to use the LimitedFPSEngine class to accomplish this. This is subclass of the default Engine. We have to override onCreateEngine and return our engine as below.
@Override public Engine onCreateEngine(EngineOptions pEngineOptions) { return new LimitedFPSEngine(pEngineOptions, 60); }
We used a value of 60 in the second constructor parameter, this is the specific amount of updates per second it will try to achieve.
Now lets create the camera, and engine options, in engine options there are basic settings like orientation, full screen move, resolution policy and camera etc.
private Camera camera; public EngineOptions onCreateEngineOptions() { camera = new Camera(0, 0, 800, 480); EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(800, 480), this.camera); engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true); engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON); return engineOptions; }
We've created a new camera, sized 800 x 480 pixels. (You can use different resolution if you wish). next we created our engine options, setting the full screen parameter to true, using fixed landscape orientation, and ratio resolution policy (if the game will be used on a different screen resolution the engine will resize our game, while keeping the proper ratio) we also set needs music and sound to true, so we will be able to use some audio for our game.
Handling back key pressed:While creating scenes, we will have to add an unimplemented method called onBackKeyPressed() - We will take care of executing it inside game activity, override the onKeyDown method.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
}
return false;
}
By doing that, the code responsible for handling actions executed after pressing the phone`s back button.
Edit android manifest file:
Open and edit android manifest file, This is how manifest looks:
Open and edit android manifest file, This is how manifest looks:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.matimdev" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.VIBRATE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <application android:label="@string/app_name" android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme"> <activity android:name=".GameActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|mcc|mnc" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
We are adding some important permissions, like internet access. (if you would like to include advertisements in your game) we also provided information about our activity. We also included configChanges property.
Now we are done with the GameActivity class and Manifest file.
No comments:
Post a Comment