如何使用phaser框架在html5中实现设备定向
how to implement device orientation in html5 with phaser framework
我正在尝试制作一个使用面向设备的API的phaser框架的简单游戏,我尝试过,但移动没有完成。这是代码`
预加载:函数(){
game.stage.backgroundColor = '#64FE2E';
game.load.image('gau','assets/ball.png');
},
create: function() {
this.ball = this.game.add.sprite(100,245, 'gau');
},
update: function() {
// Function called 60 times per second
if(this.ball.inWorld == false)
this.restart_game();
keys = this.game.input.keyboard.createCursorKeys();
window.addEventListener("deviceorientation", this.handleOrientation, true);
},
handleOrientation: function(e) {
var x = e.gamma; // range [-90,90]
var y = e.beta; // range [-180,180]
this.ball.body.velocity.x -= x*2;
this.ball.body.velocity.y -= y*4;
},`
这看起来像是一个范围问题。handleOrientation内部的"this"将指代"window"。
window.addEventListener("deviceorientation", this.handleOrientation, true);
handleOrientation: function(e) {
var x = e.gamma; // range [-90,90]
var y = e.beta; // range [-180,180]
// looking for reference to ball on the window object
this.ball.body.velocity.x -= x*2;
this.ball.body.velocity.y -= y*4;
}
一种方法是将handleOrientation方法"绑定"到当前范围,如下所示:
window.addEventListener("deviceorientation", this.handleOrientation.bind(this), true);
handleOrientation: function(e) {
var x = e.gamma; // range [-90,90]
var y = e.beta; // range [-180,180]
// this now refers to the game object
this.ball.body.velocity.x -= x*2;
this.ball.body.velocity.y -= y*4;
}
相关文章:
- 如何用HTML5地理定位API实现承诺
- HTMLUnit是否包括一个功能性的[HTML5]画布2D实现,能够将图像数据渲染回Java代码
- 了解如何在javascript/HTML5/NodeJS中实现客户端-服务器-客户端基础结构
- 如何实现 HTML5 双击点击标签
- HTML5 历史 API 实现
- 在HTML5中使用JavaScript实现图像数组的拖放
- 如何实现Mozilla的客户端JS / HTML5 pdf查看器
- 如何在HTML5中实现应用程序范围的内存缓存
- 如何使用phaser框架在html5中实现设备定向
- 在使用webgl、html5画布的情况下,使用图像实现三维图形
- 如何更改Chrome'的键盘快捷方式;实现到HTML5语音输入API
- 将图像拼贴实现为'Word'形状使用HTML5/JS
- 我应该使用哪个HTML5 Canvas库来实现此目的
- 用于可以与数据库交互的java应用程序的Html5-websocket实现
- 实现HTML5画布的魔术棒
- 如何实现HTML5 Canvas绘画应用程序的拖放
- 用Node.js实现html5模式下的Angular路由
- 是否有可能在原生Android应用程序中实现HTML5(我计划使用WebRTC)作为WebView
- 它有任何实现HTML5<keygen>MS Internet Explorer中的功能
- 如何实现html5画布的喷漆工具