【问题标题】:Phaser 3 can't get the start method to work rightPhaser 3 无法使启动方法正常工作
【发布时间】:2022-12-08 05:42:13
【问题描述】:

我正在尝试制作一个菜单,当玩家使用 start 方法单击按钮时场景会发生变化。起初,我把这一切都放在了 create 函数中:

var levelOne = this.add.sprite(200, 400, 'LevelOne').setInteractive();

    levelOne.on('pointerdown', function (pointer) {

      this.scene.start('play');
      
  });

但这导致了一个错误,它说 this.scene.start 不是一个函数。

我查看了之前该方法有效的示例,最大的区别在于该方法在更新函数中,因此我重写了我的代码以将其包含在创建函数中:

this.choice = 0;

    var levelOne = this.add.sprite(200, 400, 'LevelOne').setInteractive();

    levelOne.on('pointerdown', function (pointer) {

      this.choice = 1;
      //game.settings = {
        //gameTimer: 60000    
      //}

  });

这在更新功能中:

if (this.choice == 1){
    this.scene.start('play'); 
}

可悲的是,这也不起作用,甚至没有给出错误消息。我不知道哪里出了问题。请帮忙。

【问题讨论】:

    标签: menu phaser-framework scene


    【解决方案1】:

    您必须将上下文传递给事件函数 (link to the documentation),即第三个参数。

    levelOne.on('pointerdown', function (pointer) {
        this.scene.start('play');    
    }, this); // <-- you have to add "this"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-29
      • 2021-05-10
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多