【问题标题】:Phaser 3 scene input.on() touch events not trigerredPhaser 3 场景 input.on() 触摸事件未触发
【发布时间】:2019-09-04 13:47:08
【问题描述】:

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

   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 永远不会被调用。当我在外面监听 pointerupoutside 并点击游戏时,moveSourceSprite 被调用了。

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

我是 Phaser 的新手,一直停留在这一点上。如果您能帮助我,我将不胜感激。

谢谢,

道格

【问题讨论】:

    标签: phaser-framework


    【解决方案1】:

    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>

    【讨论】:

    • 非常感谢您的回复,非常感谢。您的示例与文档一致。但是,由于某种原因,我的点击只能被捕获为外部事件。我的意思是,不是 pointerup,而是 pointerupoutside 起作用,这让我认为命中区域存在问题。但是,我还想不通。
    • 尝试在jsfiddle中发布一些代码并分享评论我会看看它
    • 您好,Nazim,非常感谢您抽出宝贵时间。您的 JS 代码运行良好。我认为我的问题与 html 连接有关。我使用离子,所以它也可能与此有关。基本上,我定位为移相器父级的
      几乎将移相器置于后台!因此,它无法捕获点击。例如,当我为 div 设置背景颜色时,我看不到移相器场景。这是正常的吗?我玩过 z-index 但这没有帮助。你以前见过这样的事情吗?
    • 上述评论具有误导性,因为我注意到游戏对象可以捕获点击。它只是出于某种原因与场景有关......
    • 很多问题 Doug 我不知道我从哪一个开始所以我们只使用画布,这意味着你给它一个 id 或者你让 Phaser 自动将它添加到 HTML5 页面,请尝试 jsfiddle 或 codepen 最好谈谈代码 ;)
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签