First we need to install e3roid game framework. Download the latest e3roid source distribution(e3roid-source-X.X.zip) from Google code project page and unzip it to appropriate directory.
After the preparations let's create a new Android Application. At first we need a new Activity which extends the E3Activity to declare using e3roid framework. This is the base activity class of the engine.
And then override onSceneTouchEvent method and implement it like below. The example below moves the sprite on the center of the touched position. Remember that the location we can get from motion event(getX,getY) is the actual device’s raw pointer location. To adjust the coordinates that fit with the screen size defined in the E3Engine’s constructor, use getTouchEventX and getTouchEventY method.
public class OpenGLDemoActivity extends E3Activity { ... }In the onCreate() method we detect the display parameters of the mobile device because the width and height values needed to create the game engine.
@Override protected void onCreate( Bundle savedInstanceState ) { Display display = getWindowManager().getDefaultDisplay(); width = display.getWidth(); height = display.getHeight(); super.onCreate( savedInstanceState ); }The E3Engine a base engine for the framework that is responsible for rendering.
@Override public E3Engine onLoadEngine() { E3Engine engine = new E3Engine( this, width, height ); engine.requestFullScreen(); engine.requestPortrait(); return engine; }After initializing e3roid engine by overriding onLoadEngine(), we can add resources (sprite, shape, etc) by overriding onLoadResources(). The example shows how to instantiate the textures. If we use AssetTexture class, images must be saved into the “assets” folder. Transparent PNG image can be used if the sprite needs transparency. Transparent GIF image is currently not supported.
@Override public void onLoadResources() { //texture for the droid robotTexture = new AssetTexture( "gfx/droid.png", this ); //background's texture Bitmap tile = BitmapUtil.getTileBitmapFromAsset( "gfx/bg.png", 64, 64, 0, 0, 0, this ); }After initializing sprites, we can create e3roid’s “scene” and move the robot sprite into the scene. The scene width and height can be obtained by calling getWidth and getHeight. The example shows how to instantiate and add on center of the screen the sprite. After moving sprites, we must “add” the sprite to the scene.
@Override public E3Scene onLoadScene() { E3Scene scene = new E3Scene(); scene.addEventListener( this ); Background background = new Background( tile, getWidth(), getHeight(), this ); scene.getTopLayer().setBackground( background ); int centerX = (getWidth() - robotTexture.getWidth()) / 2; int centerY = (getHeight() - robotTexture.getHeight()) / 2; robot = new Sprite( robotTexture, centerX, centerY ); scene.getTopLayer().add( robot ); Toast.makeText( this, "Touch screen to move the sprite.", Toast.LENGTH_LONG ) .show(); return scene; }To handle user’s interactions, add event listener to the scene first. By adding listener to the scene, the onSceneTouchEvent method will be called when user touches the screen.
@Override public E3Scene onLoadScene() { E3Scene scene = new E3Scene(); scene.addEventListener( this ); // snip ... }
And then override onSceneTouchEvent method and implement it like below. The example below moves the sprite on the center of the touched position. Remember that the location we can get from motion event(getX,getY) is the actual device’s raw pointer location. To adjust the coordinates that fit with the screen size defined in the E3Engine’s constructor, use getTouchEventX and getTouchEventY method.
@Override public boolean onSceneTouchEvent( E3Scene scene, MotionEvent motionEvent ) { if( robot != null ) { if( motionEvent.getAction() == MotionEvent.ACTION_DOWN ) { int x = getTouchEventX( scene, motionEvent ) - (robotTexture.getWidth() / 2); int y = getTouchEventY( scene, motionEvent ) - (robotTexture.getHeight() / 2); robot.move( x, y ); } } return false; }The result should look like this: