使用外部按钮在Phaser中触发动作

时间:2018-09-12 21:55:11

标签: typescript button phaser-framework

我当前遇到一个问题:我想让我的玩家精灵(左角色)移动到右角色。我已经可以使用它了,但是现在我想在按下按钮时使角色移动,这是我的问题,我不知道该怎么做。

我已经尝试过使用发射器和外部bool。

这是html的外观: enter image description here

这是代码的一部分

    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
}

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

1 个答案:

答案 0 :(得分:0)

由于Phaser是JavaScript,因此您可以通过标准JavaScript功能将事件监听器添加到按钮。

在您的create()中,您可以添加事件监听器,如下所示:

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