0

I'm trying to get an Android program to recognize a gaming controller for the Ouya. I can make the following code work when it is alone, but I am using a framework that I would like to plug it into. I have tried a few different things (instantiating the class in another one, etc., but so far what I have tried hasn't worked. Can you help me know what to do to make this into its own class and have it take action when someone uses the controller? Thank you!

Here is my code:

import tv.ouya.console.api.OuyaController;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class SampleGame extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OuyaController.init(this);
    }

    @Override
    public boolean onKeyDown(final int keyCode, KeyEvent event){
        //Get the player #
        int player = OuyaController.getPlayerNumByDeviceId(event.getDeviceId());       
        boolean handled = false;

        //Handle the input
        switch(keyCode){
            case OuyaController.BUTTON_O:
                System.out.println("Button 'O' was pressed.");
                //doSomethingWithKey();
                handled = true;
                break;
            case OuyaController.BUTTON_U:
                System.out.println("Button 'U' was pressed.");
                //doSomethingWithKey();
                handled = true;
                break;
            case OuyaController.BUTTON_Y:
                System.out.println("Button 'Y' was pressed.");
                //doSomethingWithKey();
                handled = true;
                break;
            case OuyaController.BUTTON_A:
                System.out.println("Button 'A' was pressed.");
                //doSomethingWithKey();
                handled = true;
                break;
        }
        return handled || super.onKeyDown(keyCode, event);
    }

}
bstrong
  • 680
  • 9
  • 20
  • 1
    What framework are you trying to integrate? Here's how I setup for raw HTML5 & a webview: https://github.com/psema4/TicTacToe/blob/master/src/com/psema4/tictactoe/MainActivity.java – psema4 Aug 30 '13 at 03:07
  • It should be noted however that I am not really a Java programmer... After much pain I hacked this together. Pull request would be most welcome ;-) – psema4 Aug 30 '13 at 03:08
  • 1
    It's the framework used in the Kilobolt tutorial. The framework itself is from the book Beginning Android Games by Mario Zechner and Robert Green. The framework was downloaded from http://www.kilobolt.com/create-an-android-game-from-scratch-or-port-your-existing-game.html. – bstrong Aug 30 '13 at 03:14
  • @psema4 Thanks for the link to your project. I'll have to take a look at it when I'm at my desktop (with git). – bstrong Aug 30 '13 at 03:26

1 Answers1

0

You need to use the onGenericMotionEvent(MotionEvent event) method that is part of the Ouya SDK.

This gives you access to both the analogue sticks and all the other controls.

user1418706
  • 202
  • 1
  • 11