2. Your first app

To create your first app, extend your Game class with the class EnvMobileGame.  Like so:

	import env3d.android.EnvMoblieGame;

	public class Game extends EnvMobileGame {

You need to override 2 abstract methods to extend EnvMobileGame, setup() and loop().  In the desktop version of env3d, you put your initialization code in the constructor, and in the play method, you put the main game loop. 

In the android version, all the setup code needs to go inside the setup() method.  There is also no need to write an explicit loop. Instead the loop() method is called automatically by the env3d framework.  Let's start with the most basic example:

import env3d.android.EnvMobileGame;
import env3d.advanced.EnvNode;

public class Game extends EnvMobileGame
{
    private EnvNode o;
    
    public void setup()     
    {
        o = new EnvNode();
        // The object is a sphere at location 0, 0, 0
        env.addObject(o);
    }

    public void loop() 
    {        
        o.setRotateY(o.getRotateY()+1);
    }

    public static void main(String[] args) 
    {
        Game g = new Game();
        g.start();
    }
}

In the above example, I created a default object and put it inside the environment in the setup method (a default EnvNode object is a sphere with the earth.png as texture). In the loop method, this object is being rotated by 1 degree each frame. The loop method is called 30 times per second.

The use of the main method makes it easy to launch the game on the PC instead of having to upload to the device for testing purposes. 

 

Differences from the desktop version

In the desktop version, the game loop must be written explicitly.  The android version, in contrast, need to override the setup() and loop() method.  The game loop is called implicitly by the env3d framework and the loop() method is called every frame (30 times per second).

  • There is no need to create the env object.  The env object is a protected field and is created by the  EnvMobileGame class.
  • There is no default "room", just a black background.
  • By default, the camera is located at (0, 0, 10).  Just like the desktop couterpart, you can change the camera location by calling the setCameraX, setCameraY, and setCameraZ methods of the env object.
  • There is a default on-screen keyboard.  We will learn how to get touch input in the next lesson.
  • The EnvMoblieGame class has a start() method that can be called to start your app.  The inclusion of the main method makes it easy to run your app on your PC instead of having to upload your game to a device everytime you want to test your program. 
  • The only way to stop the running program is to right-click on the "running bar" in BlueJ and select "Reset Java Virtual Machine".

Using the Scene Creator

Instead of writing your own Game class and creating your own objects, the "Scene Creator" can also be used to create android apps.  Simply launch the "Scene Creator" from the env3d menu and create your scene.  When you exit or save, you will be given the option to choose between the android or desktop version.

The Scene Creator gives users of env3d the ability to use a drag-and-drop environment to create their own app with very little coding!

Exercise

  1. Besides the above differences, env3d still works pretty much the same way.  Try to create a "room" and put a boncing ball in the room.
  2. The env object created by EnvMobileGame belongs to the EnvAdroid class.  EnvAdroid class contains many methods similar to the regular Env class, but is specialized for mobile development.  Study the Javadoc for the EnvAdroid class and identify the differences from the regular Env class.
  3. Try creating an app using the Scene Creator.