首页 > 解决方案 > Phaser 3 场景 input.on() 触摸事件未触发

问题描述

我在角度组件(离子项目)的构造函数中有以下设置。

   let config = {
      type: Phaser.AUTO,
      width: '100vw',      
      height: '100vh',
      transparent:true,
      disableContextMenu: true,
      parent: 'phaser-example',
      physics: {
          default: 'arcade',
          arcade: {
              gravity: { y: 200 }
          }
      },
      scene: {
        preload: function() {
          that.preload(this);
        },
        create: function() {
          that.create(this);
        },
        update: function() {
          that.update(this);
        }
      }
    };

    this.game = new Phaser.Game(config);    

我在 create 方法中为单击/触摸事件设置了一个侦听器,如下所示:

create(scene){
   scene.input.on('pointerup', this.moveSourceSprite, scene);      
}

但是,永远不会调用 moveSourceSprite。当我在外面收听指针并单击游戏时,会调用 moveSourceSprite。

我想,由于某种原因,场景没有听整个游戏区域。

我对 Phaser 是全新的并且停留在这一点上。如果你能帮助我,我将不胜感激。

谢谢,

道格

标签: phaser-framework

解决方案


var myScene1 = {
    key:'scene1',
    preload: function(){
      this.load.image('alien1', 'sprites/phaser-alien.png');;
    },
    create: function(){
      this.add.image(100, 100, 'alien1');
      this.input.on('pointerup', ()=>{game.scene.start('scene2'); game.scene.stop('scene1')}, myScene1);
    },
}

var myScene2 = {
    key:'scene2',
    preload: function(){
      this.load.image('alien2', 'sprites/alien2.png');;
    },
    create: function(){
      this.add.image(100, 100, 'alien2');
      this.input.on('pointerup', ()=>{game.scene.start('scene1'); game.scene.stop('scene2')}, myScene2);
    },
}

var config = {
    type: Phaser.AUTO,
    width: 600,
    height: 600,
    loader: {
    baseURL: 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/',
      crossOrigin: 'anonymous'
    },
    parent: 'phaser-example',
    scene: [myScene1, myScene2]
};

var game = new Phaser.Game(config);
game.scene.start('scene1');
<script src="//cdn.jsdelivr.net/npm/phaser@3.19.0/dist/phaser.js"></script>


推荐阅读