Controller support?


(Nate ) #1

Hey guys! I was just wondering if I am able to map some key strokes for use with my PS3 controller using FlashDevelop and flash punk!

I have a website where I can host the .swf file, then on my ps3 I can navigate to the site and play the .swf. However I am not sure how I would go about mapping some keys for the ps3 controller!

Any help would be greatly appreciated!

Thanks!


(Jacob Albano) #2

This is only supported on PC with a program like Joy2Key or Autohotkey. If you compile to AIR you can use Adobe’s game controller class, but as far as I know it only works with a few android devices.


(Nate ) #3

Okay thank you Jacob! :smiley:


(Ultima2876) #4

Adobe’s GameInput class also works perfectly on desktop if you compile against AIR 3.8 beta. Check out this library to get going with it: https://github.com/arkeus/as3-controller-input

As far as I know, this also applies to web-deployed flash games - but don’t quote me on that!

(having ps3 controls won’t work either way, unfortunately).


(Jacob Albano) #5

Interesting! Last I heard it was incredibly limited. Unfortunately it still won’t work on Linux as the last release of Air for Linux was 2.8, I think? Still a lot better than it used to be.


(Nate ) #6

My current project is not in AIR, just straight up AS3, will this be an issue?


(Ultima2876) #7

True that - Linux support for flash development is basically nixed. You’re better using Haxe for that.


(rostok) #8

Guys here’s my quick patch with AIR support for game controllers. I do know it is pretty ugly so don’t make fun of it.

package  
{
	import flash.events.*;
	import flash.events.Event;
	import flash.events.GameInputEvent;
	import flash.ui.GameInput;
	import flash.ui.GameInputControl;
	import flash.ui.GameInputDevice;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import net.flashpunk.utils.Input;
	
	/**
	 * FlashPunks extension to GameControllers based on http://www.adobe.com/devnet/air/articles/game-controllers-on-air.html 
	 * this static object requires Input to provide public static method overrideInput() provided here for reference
	 * 
	 * 	public static function overrideInput(type:String, evnt:*):void 
	 *	{
	 *		switch (type) {
	 *		case KeyboardEvent.KEY_DOWN: 
	 *			for each (var k:int in keys(evnt)) {
	 *				onKeyDown(new KeyboardEvent(type, true, false, 0, k));
	 *				break;
	 *			}
	 *			break;
	 *		case KeyboardEvent.KEY_UP: 	
	 *			for each (var k:int in keys(evnt)) {
	 *				onKeyUp(new KeyboardEvent(type, true, false, 0, k));
	 *				break;
	 *			}
	 *			break;
	 *		}
	 *	}
	 *
	 * @author rostok
	 */
	public class InputGamepad 
	{
		private static var gameInput:GameInput;
		private static var timer:Timer;
		private static var timerStop:Timer;
		private static var _device:GameInputDevice;
		
		public static var controls:Vector.<GameInputControl> = new Vector.<GameInputControl>;
		public static var definesDown:Dictionary = new Dictionary();
		public static var definesUp:Dictionary = new Dictionary();
		
		public function InputGamepad() 
		{
			
		}
		
		/**
		 * defines mapping of gamecontroller input to FP's Input. 
		 * If value will reach 0 it will trigger KEY_UP/release event 
		 * @param	name	a Input string for example "up"
		 * @param	control	name of gamepad control for example "AXIS_1"
		 * @param	value	value that if equal will trigger KEY_DOWN
		 */ 
		public static function define(name:String, control:String, value:Number):void 
		{
			definesDown[control + value] = name;
			var x:Number = 0;
			definesUp[control + x + value] = name;
		}
		
		/**
		 * this needs to be run in order to init gameinput
		 */
		public static function initialize():void 
		{
			gameInput = new GameInput();
			gameInput.addEventListener(GameInputEvent.DEVICE_ADDED, handleDeviceAttached);
			gameInput.addEventListener(GameInputEvent.DEVICE_REMOVED, handleDeviceRemoved);
		}
		
		protected static function handleDeviceRemoved(event:GameInputEvent):void
		{
			trace("Device is removed\n");
		}
		
		protected static function handleDeviceAttached(e:GameInputEvent):void
		{
			trace("Device is added\n");
			
			for(var k:Number=0;k<GameInput.numDevices;k++){
				_device = GameInput.getDeviceAt(k);
				var _controls:Vector.<String> = new Vector.<String>;
				_device.enabled = true;
				
				for (var i:Number = 0; i < _device.numControls; i++) {
					var control:GameInputControl = _device.getControlAt(i);
					control.addEventListener(Event.CHANGE, onChange);
					_controls[i] = control.id;
					controls[i] = control;
				}
			}
		}	
		
		protected static function onChange(event:Event):void
		{
			var control:GameInputControl = event.target as GameInputControl;
			//trace("The pressed control is " +control.id + " with value " + control.value + " \n");
			var d:String;
			d = definesDown[control.id + control.value];
			if (d) 
			{ 
				Input.overrideInput(KeyboardEvent.KEY_DOWN, d);
				//trace(control.id + control.value, "=>", d);
			}
			d = definesUp[control.id + control.value + control.minValue];
			if (d) 
			{ 
				Input.overrideInput(KeyboardEvent.KEY_UP, d); 
				//trace(control.id + control.value, "=>", d);
			}
			d = definesUp[control.id + control.value + control.maxValue];
			if (d) 
			{ 
				Input.overrideInput(KeyboardEvent.KEY_UP, d); 
				//trace(control.id + control.value, "=>", d);
			}
		}
	}
}

To use it just add it anywhere in your project.Initialization is pretty straitforward:

	InputGamepad.initialize();
	InputGamepad.define("up", "AXIS_1", -1);
	InputGamepad.define("down", "AXIS_1", 1);
	InputGamepad.define("left", "AXIS_0", -1);
	InputGamepad.define("right", "AXIS_0", 1);
	InputGamepad.define("1", "BUTTON_9", 1);
	InputGamepad.define("2", "BUTTON_10", 1);
	InputGamepad.define("3", "BUTTON_11", 1);
	InputGamepad.define("4", "BUTTON_12", 1);

(Ultima2876) #9

Pretty sweet. I’m wondering if we can rework the overrideInput function into something that would feel more natural as a part of the main FlashPunk branch, then patch it in to provide better support for these kinds of things.


(Alex Larioza) #10

The GameInput class requires Air 3.7, you can’t do it in native AS3. Also note that the newest version produces A LOT of overhead because of a rogue timer.


(Zachary Lewis) #11

lol this code looks like a ugly dork what loser code


(Andrew Stewart) #12

Hi

Has anyone managed to get this working? I get the following error when I try to run it:

src\io\arkeus\ouya\control\GameControl.as(29): col: 47 Error: Access of possibly undefined property value through a reference with static type flash.ui:GameInputControl.

Can anyone help?