Skip to content

Instantly share code, notes, and snippets.

@jgranick
Last active December 19, 2015 03:09
Show Gist options
  • Save jgranick/5888202 to your computer and use it in GitHub Desktop.
Save jgranick/5888202 to your computer and use it in GitHub Desktop.
Adding controller support for OUYA
package;
import flash.display.Sprite;
import flash.geom.Point;
import openfl.events.JoystickEvent;
#if android
import openfl.utils.JNI;
import tv.ouya.console.api.OuyaController;
#end
class Main extends Sprite {
public function new () {
super ();
#if android
var getContext = JNI.createStaticMethod ("org.haxe.nme.GameActivity", "getContext", "()Landroid/content/Context;", true);
OuyaController.init (getContext ());
#end
stage.addEventListener (JoystickEvent.AXIS_MOVE, stage_onJoystickAxisMove);
stage.addEventListener (JoystickEvent.BUTTON_DOWN, stage_onJoystickButtonDown);
stage.addEventListener (JoystickEvent.BUTTON_UP, stage_onJoystickButtonUp);
stage.addEventListener (JoystickEvent.HAT_MOVE, stage_onJoystickHatMove);
}
// Event Handlers
private function stage_onJoystickAxisMove (event:JoystickEvent):Void {
var player = OuyaController.getPlayerNumByDeviceId (event.device);
var leftX = event.x; // event.axis[0];
var leftY = event.y; // event.axis[1];
var rightX = event.axis[3];
var rightY = event.axis[4];
}
private function stage_onJoystickButtonDown (event:JoystickEvent):Void {
var player = OuyaController.getPlayerNumByDeviceId (event.device);
var buttonCode = event.id;
}
private function stage_onJoystickButtonUp (event:JoystickEvent):Void {
var player = OuyaController.getPlayerNumByDeviceId (event.device);
var buttonCode = event.id;
}
private function stage_onJoystickHatMove (event:JoystickEvent):Void {
var player = OuyaController.getPlayerNumByDeviceId (event.device);
var left = (event.x < 0);
var right = (event.x > 0);
var up = (event.y < 0);
var down = (event.y > 0);
}
}
@Hasufel
Copy link

Hasufel commented Jun 30, 2013

I implemented the poll function and more - and simplified the overall process - we can ditch getPlayerNumByDeviceId totally. http://www.nme.io/download_file/592/8014/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment