3. On-screen controller

By default, the on-screen controller is displayed.  It includes 6 buttons, as follows:

 

 

Interacting with the controller

There is only one method to call:  Env.getKeyDown(String).  It returns a boolean indiciating if the key is being held down or not.  The parameter tells the method which key to press.  Let's use our previous simple example and add one simple line to detect key "a" being pressed down:

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() 
    {        
        // Only spin the object when "a" button is pressed
        if (env.getKeyDown("a")) {
            o.setRotateY(o.getRotateY()+1);
        }
    }
 
    public static void main(String[] args) 
    {
        Game g = new Game();
        g.start();
    }
}

If you run the above program on a PC, the mouse button emulates touch.  Multi-touch cannot be emulated but runs fine on a device. 

 

Modifying the on-screen controller

The default controller is handy, but you may want something for your own needs.  To customize your own controller, look inside the project's textures/controller directory.  You'll see 3 files:

  • buttons.png - a png with transparent background.  This picture will be displayed as the on-screen controller.
  • layout.txt - a text file containing the names and coordinates of all the buttons.
  • touched.png - a transparent png which will be super-imposed over the buttons that is being touched.  Gives the user some visual feedback when a button is being pressed.

Of the 3 files, the layout.txt is most interesting as it defines the areas where a user can pressed.  The default layout.txt looks like this:

up, 126, 142, 215, 230
down, 128, 372, 216, 457
left, 16, 256, 105, 345
right, 244, 255, 329, 345
a, 499, 363, 586, 451
b, 665, 363, 747, 454

Each line in the layout.txt file represents a button.  You can define as many buttons as you want.  There are 5 fields on each line, separated by commas.  The fields are:

  • name - the name of the button, this name will be used in your call as the parameter to Env.getKeyDown(String) to detect if that portion of the screen is being touched.
  • top-left x coordinate
  • top-left y coordinate 
  • botton-right x coordinate
  • botton-right y coordinate

After the name field, we define a rectangler area with 2 sets of coordinates (top-left, botton-right).  When that area is being touched, the method Env.getKeyDown(String) would return true.  The coordinates are based on the image bottons.png.

 

Turning off the controller

The controller can be turned off (hidden) by calling Env.setShowController(false).  When is controller is not shown, none of the buttons would work.

 

Exercise

  1. Use scene creator to create an app where you can use the arrow keys of the on-screen controller to control the movement of a character, as follows:

 

AttachmentSize
buttons.png33.36 KB