Skip to content

Instantly share code, notes, and snippets.

@jgranick
Created February 9, 2012 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgranick/1784122 to your computer and use it in GitHub Desktop.
Save jgranick/1784122 to your computer and use it in GitHub Desktop.
How to access a joystick/gamepad (NME recipe)
package com.joshuagranick.joystickexample;
import nme.display.Sprite;
import nme.events.JoystickEvent;
import nme.Lib;
/**
* @author Joshua Granick
*/
class JoystickExample extends Sprite {
public function new () {
super ();
Lib.current.stage.addEventListener (JoystickEvent.AXIS_MOVE, stage_onJoyAxisMove);
Lib.current.stage.addEventListener (JoystickEvent.BALL_MOVE, stage_onBallMove);
Lib.current.stage.addEventListener (JoystickEvent.BUTTON_DOWN, stage_onJoyButtonDown);
Lib.current.stage.addEventListener (JoystickEvent.BUTTON_UP, stage_onJoyButtonUp);
Lib.current.stage.addEventListener (JoystickEvent.HAT_MOVE, stage_onJoyHatMove);
}
private function stage_onBallMove (event:JoystickEvent):Void {
// event.device -- specifies which input device is reporting the event
// event.id -- specifies which trackball is reporting the event
// event.x -- relative X movement
// event.y -- relative Y movement
}
private function stage_onJoyAxisMove (event:JoystickEvent):Void {
// event.device -- specifies which input device is reporting the event
// event.x -- x axis value, from -1 to 1
// event.y -- y axis value, from -1 to 1
// event.z -- z axis value, from -1 to 1
}
private function stage_onJoyButtonDown (event:JoystickEvent):Void {
// event.device -- specifies which input device is reporting the event
// event.id -- specifies which button is reporting the event
}
private function stage_onJoyButtonUp (event:JoystickEvent):Void {
// event.device -- specifies which input device is reporting the event
// event.id -- specifies which button is reporting the event
}
private function stage_onJoyHatMove (event:JoystickEvent):Void {
// event.device -- specifies which input device is reporting the event
// event.id -- specifies which button is reporting the event
// event.x -- specifies the x position of the hat, either -1, 0 or 1
// event.y -- specifies the y position of the hat, either -1, 0 or 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment