Virtual Gamepad Example?

Hey i have a question about virtual gamepad. How can i realize this? has someone an good example for that?

Do you have an example of what it is you’re trying to do?

1 Like

I just want to make some simple buttons that can be interacted with onAction(), i need this for an android game. just to make some simple buttons, but i really dont know how to start :frowning:

Sorry I can’t share my code for you now but let’s take a look of this code, I learned from that:

http://hub.jmonkeyengine.org/groups/android/forum/topic/getting-android-input-to-work/



[java]package sg.edu.sp.dmit.ANBDemo;



import android.util.Log;



import com.jme3.animation.AnimChannel;

import com.jme3.animation.AnimControl;

import com.jme3.animation.AnimEventListener;

import com.jme3.animation.LoopMode;

import com.jme3.app.SimpleApplication;

import com.jme3.collision.CollisionResult;

import com.jme3.collision.CollisionResults;

import com.jme3.font.BitmapText;

import com.jme3.input.KeyInput;

import com.jme3.input.MouseInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.input.controls.MouseButtonTrigger;

import com.jme3.input.controls.TouchListener;

import com.jme3.input.controls.TouchTrigger;

import com.jme3.input.event.TouchEvent;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Ray;

import com.jme3.math.Vector2f;

import com.jme3.math.Vector3f;

import com.jme3.renderer.Renderer;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.Spatial;

import com.jme3.scene.shape.Box;

import com.jme3.scene.shape.Quad;

import com.jme3.scene.shape.Sphere;

import com.jme3.ui.Picture;



public class Test17 extends SimpleApplication implements TouchListener,AnimEventListener {



private AnimChannel channel;

private AnimControl control;

Node player;



@Override

public void simpleInitApp() {



initKeys(); // load custom key mappings



settings.setHeight(480);

settings.setWidth(800);



// create my picture buttons

Picture pic = new Picture(“HUD Picture”);

pic.setImage(assetManager, “Textures/ColoredTex/Monkey.png”, true);

pic.setWidth(80f);

pic.setHeight(80f);

pic.setPosition(0f, 0f);

guiNode.attachChild(pic);



Picture pic2 = new Picture(“HUD Picture”);

pic2.setImage(assetManager, “Textures/ColorRamp/cloudy.png”, true);

pic2.setWidth(80f);

pic2.setHeight(80f);

pic2.setPosition(85f, 0f);

guiNode.attachChild(pic2);



Picture pic3 = new Picture(“HUD Picture”);

pic3.setImage(assetManager, “Textures/ColorRamp/toon.png”, true);

pic3.setWidth(80f);

pic3.setHeight(80f);

pic3.setPosition(170f, 0f);

guiNode.attachChild(pic3);



DirectionalLight dl = new DirectionalLight();

dl.setDirection(new Vector3f(-0.1f, -1f, -1).normalizeLocal());

rootNode.addLight(dl);



player = (Node) assetManager.loadModel(“Models/Oto/Oto.mesh.xml”);

player.setLocalScale(0.5f);

rootNode.attachChild(player);



control = player.getControl(AnimControl.class);

control.addListener(this);

channel = control.createChannel();

channel.setAnim(“stand”);



}



/** Declaring the “Shoot” action and mapping to its triggers. */

private void initKeys() {



inputManager.addMapping(“Touch”, new TouchTrigger(0));

inputManager.addListener(this, new String[]{“Touch”});

}



public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {

if (animName.equals(“Walk”)) {

channel.setAnim(“stand”, 0.50f);

channel.setLoopMode(LoopMode.DontLoop);

channel.setSpeed(1f);

}

if (animName.equals(“pull”)) {

channel.setAnim(“stand”, 0.50f);

channel.setLoopMode(LoopMode.DontLoop);

channel.setSpeed(1f);

}

if (animName.equals(“Dodge”)) {

channel.setAnim(“stand”, 0.50f);

channel.setLoopMode(LoopMode.DontLoop);

channel.setSpeed(1f);

}

}



@Override

public void onTouch(String binding, TouchEvent evt, float tpf) {

float x;

float y;

x = evt.getX();

y = evt.getY();

switch(evt.getType())

{

case MOVE:



break;



case TAP:



break;



case DOWN:



if (x>0&&x<80&&y>0&&y<80){

Log.e("",“You touched " + x + " " + y + " which is button 1!”);



if (!channel.getAnimationName().equals(“Walk”)) {

channel.setAnim(“Dodge”, 0.50f);

channel.setLoopMode(LoopMode.Loop);

}

}

if (x>85&&x<165&&y>0&&y<80){

Log.e("",“You touched " + x + " " + y + " which is button 2!”);

if (!channel.getAnimationName().equals(“Walk”)) {

channel.setAnim(“pull”, 0.50f);

channel.setLoopMode(LoopMode.Loop);

}

}

if (x>170&&x<250&&y>0&&y<80){

Log.e("",“You touched " + x + " " + y + " which is button 3!”);



if (!channel.getAnimationName().equals(“Walk”)) {

channel.setAnim(“Walk”, 0.50f);

channel.setLoopMode(LoopMode.Loop);

}



}



break;



case UP:



break;



case FLING:

break;



default:



break;

}

Log.e("",“Event Type " + evt.getType() + " x=”+x+" y="+y);

evt.setConsumed();



}



@Override

public void onAnimChange(AnimControl arg0, AnimChannel arg1, String arg2) {

// TODO Auto-generated method stub



}

}[/java]

As soon as I complete my tutorials here

http://hub.jmonkeyengine.org/groups/free-announcements/forum/topic/rpg-game-making-tutorials-series-by-atomix/

You can find the JME3 gamepad in my SVN :stuck_out_tongue:

http://code.google.com/p/blade-of-immortals-rpg-jme3/



Cheers,

This is pretty basic NiftyGUI functionality if I understand you correctly…



https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:nifty_gui

1 Like

One more thing:

I use nifty to display the Left - Right - Up - Down Arrow (on the Left of the screen) but not use Nifty to capture input Event… :stuck_out_tongue: I also want to know how to capture such Touch event on Android with Nifty.

thank you atomix! with this example i can work :wink:

Really good examples here: https://github.com/latestpost/JMonkey3-Android-Examples

@akramfares said:
Really good examples here: https://github.com/latestpost/JMonkey3-Android-Examples

gha, thats why people include the android logger :/ thats why I don't like external example repositories.. people come to us and complain.