Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. pc.script.create('motioninput', function(app){
  2.  
  3. // Creates a new MotionInput instance
  4. var MotionInput = function (entity) {
  5. this.entity = entity;
  6. };
  7.  
  8. var alpha = 0;
  9. var beta = 0;
  10. var gamma = 0;
  11.  
  12. var xAcceleration = 0;
  13. var yAcceleration = 0;
  14. var zAcceleration = 0;
  15.  
  16. MotionInput.prototype = {
  17.  
  18. // Called once after all resources are loaded and before the first update
  19. initialize: function () {
  20.  
  21. var app = this.app;
  22.  
  23. window.addEventListener("devicemotion", this.handleMotion, true);
  24. window.addEventListener("deviceorientation", this.handleOrientation, true);
  25.  
  26. var hudDiv = document.createElement('div');
  27. hudDiv.style.position = 'absolute';
  28. hudDiv.style.width = '500px';
  29. hudDiv.style.top = '0px';
  30. hudDiv.style.left = '50%';
  31. hudDiv.style.marginLeft = '-250px';
  32. hudDiv.style.textAlign = 'center';
  33. hudDiv.style.color = 'white';
  34. hudDiv.style.fontFamily = 'Audiowide';
  35.  
  36. var creditsDiv = document.createElement('div');
  37. creditsDiv.style.position = 'block';
  38. creditsDiv.style.fontSize = 'small';
  39. hudDiv.appendChild(creditsDiv);
  40. this.creditsDiv = creditsDiv;
  41.  
  42. var container = document.body;
  43. container.appendChild(hudDiv);
  44. },
  45.  
  46. handleMotion : function (ev){
  47. xAcceleration = ev.acceleration.x;
  48. yAcceleration = ev.acceleration.y;
  49. zAcceleration = ev.acceleration.z;
  50. },
  51.  
  52. handleOrientation : function (ev) {
  53. alpha = ev.alpha;
  54. beta = ev.beta;
  55. gamma = ev.gamma;
  56. },
  57.  
  58.  
  59. // update code called every frame
  60. update: function(dt) {
  61. var app = this.app;
  62.  
  63. // var msg = 'alpha: ' + alpha.toFixed(1) + ' ' + 'beta: ' + beta.toFixed(1) + ' ' + 'gamma: ' + gamma.toFixed(1) + ' x: ' + xAcceleration.toFixed(1) + ' y: ' + yAcceleration.toFixed(1) + ' z: ' + zAcceleration.toFixed(1);
  64. if(xAcceleration === null) return;
  65. var msg = ' x: ' + xAcceleration.toFixed(1) + ' y: ' + yAcceleration.toFixed(1) + ' z: ' + zAcceleration.toFixed(1);
  66. this.creditsDiv.innerHTML = msg;
  67. },
  68.  
  69. // swap method called for script hot-reloading
  70. // inherit your script state here
  71. swap: function(old) {
  72.  
  73. }
  74.  
  75. };
  76.  
  77. return MotionInput;
  78. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement