【问题标题】:Trigger action in Phaser with an external button使用外部按钮在 Phaser 中触发操作
【发布时间】:2019-02-17 14:48:04
【问题描述】:

我目前面临一个问题:我想让我的玩家精灵(左侧角色)移动到右侧。我已经让它工作了,但现在我想让角色在我按下按钮时移动,这是我的问题,我不知道该怎么做。

我已经尝试过使用发射器和外部布尔值。

这是 html 的外观:

这是部分代码

    export class CombatComponent implements OnInit {

constructor() { }

public onGameReady(game: Phaser.Game): void {
   this.game = game;
}

public game: Phaser.Game;

public readonly gameConfig: Phaser.Config = {
title: environment.title,
type: Phaser.AUTO,
width: 1024,
height: 512,
physics: {
  default: 'arcade',
  arcade: {
    gravity: {y: 300},
    debug: false
  }
},
scene: {
  preload: function() {
    ...
  },
  create: function () {
   ....
  },
  update: function() {

   ....

  }
}
};

attack() {
 //would like to make my character move here like this.player.x += 1
}

如果有人有解决方案或提示,我很乐意阅读!

【问题讨论】:

    标签: typescript button phaser-framework


    【解决方案1】:

    由于 Phaser 是 JavaScript,您可以通过标准 JavaScript 功能向按钮添加事件侦听器。

    在您的create() 中,您可以像这样添加事件监听器:

    let externalButton =  document.getElementById("yourButtonId");
    if (externalButton) {
        externalButton.addEventListener("click", this.attack);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-04-22
      • 1970-01-01
      • 2019-10-18
      • 2015-04-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多