相位抖动检测
Phaser shake detection
我在玩phaserjs和cordova,我被困在一些可能微不足道的东西。这是一个比相位器更普遍的javascript问题,但我找不到任何答案。
我使用Cordova-plugin-shake进行震动检测,效果很好。我想做的是,在shake()上改变一些精灵。
Test.Game.prototype = {
create: function() {
this.hand = this.game.add.sprite(this.game.world.centerX-36, 27, 'hand');
this.hand.animations.add('squeeze',[0,1,2,3,4,5,6,5,4,3,2,1,0]);
this.hand.animations.add('shake',[8,9,10,11,12,13,14,15,16,17,17,8,9,8]);
this.healthBar = this.game.add.sprite(260, 18, 'utils');
this.setCapacity(4);
shake.startWatch(this.onShake, 40);
},
onShake: function(){
console.log("shaked");
this.hand.animations.play('shake', 60, false);
this.setCapacity(Math.floor(Math.random() * (MAX_CAPACITY - MIN_CAPACITY + 1)) + MIN_CAPACITY);
},
setCapacity: function(capacity){
this.healthBar.prevCap = capacity;
this.healthBar.inCapacity = capacity;
this.healthBar.capacity = capacity;
var cropRect = new Phaser.Rectangle(0, 0, healthBarWidth, this.healthBar.height);
this.healthBar.crop(cropRect);
this.healthBar.prevWidth = healthBarWidth;
},
[...]
};
问题是onShake是按值传递的,对吧?所以我不能访问setCapacity()或hand。我怎样才能避免呢?有没有像这样的教程/例子,我可以阅读?
谢谢你的时间,很抱歉这样一个微不足道的问题,但我仍然是非常新的js。
你应该能够做到这一点
shake.startWatch(this.onShake.bind(this), 40);
bind方法用于将上下文附加到新函数
如果你不能使用bind,你可以创建一个闭包
shake.startWatch((function(game){
return function(){
game.onShake();
};
})(this), 40);
相关文章:
- 如何检测是否有溢出
- 如何检测用于WebGL的专用或集成显卡
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- 如何使用密码检测网络中的状态连接
- 使用JavaScript检测Flash
- 在不阻止默认行为的情况下检测IE10中的缩放
- 检测个位数整数时正在转换毫秒
- 检测电话窃听,即:<a href="电话:xxx">在UIWebview上
- 动态插入的表:JQuery未检测到最后一行
- 检测页面上某个元素中选择(突出显示)或单击的内容
- jQuery检测浏览器是否支持Zoom
- 检测iframe是否跨域的愚蠢方法
- 检测图像分辨率
- 正在检测html5手机中的抖动
- 如何计算使用javascript检测到的抖动数量
- 我们可以对可观察流使用高通滤波器来检测抖动事件吗
- 相位抖动检测
- 使用加速度计检测抖动,然后激活老虎机
- Android浏览器抖动检测与javascript的web应用程序