【问题标题】:Phaser shake detection移相器抖动检测
【发布时间】:2015-10-23 16:54:16
【问题描述】:

我在玩phaserjs 和cordova,但我遇到了一些可能微不足道的事情。这是比 Phaser 更一般的 javascript 问题,但我找不到任何答案。

我使用Cordova-plugin-shake 进行抖动检测,效果很好。我想做的是改变一些精灵 onShake()。

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() 或手。我怎样才能避免它?有没有我可以阅读的教程/示例?

感谢您抽出宝贵时间,很抱歉问了这么一个微不足道的问题,但我对 js 还是很陌生。

【问题讨论】:

  • “Phaser”的标签描述告诉我,它是 Java 7 中的一个工具。该标签与 JavaScript 问题有什么关系?
  • 不,它是phaser.js,它是一个javascript游戏框架。
  • 那是错误的标签——我把它改成了 JS 框架标签。
  • 抱歉,没有注意到它有不同的标签

标签: javascript cordova cordova-plugins phaser-framework


【解决方案1】:

你应该可以做到的

shake.startWatch(this.onShake.bind(this), 40);

bind 方法用于将上下文附加到新函数

或者如果你不能使用绑定,你可以做一个闭包

 shake.startWatch((function(game){
     return function(){
         game.onShake();
     };
 })(this), 40);

【讨论】:

  • 绑定工作正常!谢谢你也让我知道关闭!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-01
  • 2016-04-18
  • 2015-04-03
相关资源
最近更新 更多