Using Gamepads in Google Dart

2019-07-02 04:07发布

问题:

I'm looking for code examples for the Gamepad API for Google Dart.

I've tried relying on the API doc directly and attempted to write out an instance for it.

//Gamepad Example:

Gamepad g = new Gamepad();
g.id.toString(); // This doesn't seem to return anything...

If anyone has any code examples for this that would be fantastic!

回答1:

I tried this and it worked for me :

import "dart:html";

main(){
  window.animationFrame.then(runAnimation);
}   

runAnimation(timestamp){

  var gamepads = window.navigator.getGamepads();

  for (var pad in gamepads)
  {
    if(pad != null){
      querySelector("#buttons").setInnerHtml("${pad.buttons.join(" ")}</br>${pad.axes.join(" ")}");
    }
  }

  window.requestAnimationFrame(runAnimation);
}


回答2:

I don't have a gamepad to try but

Gamepad g = new Gamepad(); 

doesn't work because Gamepade has no public constructor.
Try

var gp = window.navigator.getGamepads();

//or

// haven't tried because I don't know how to provoke this event without a gamepad
window.on['gamepadconnected'].listen((e) {
  var gamepad = e.gamepad;
});

instead.
I get an empty list so I can't examine more.

see also:
- https://developer.mozilla.org/en-US/docs/Web/Guide/API/Gamepad
- https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html