【问题标题】:Phaser: Attach Touch Event on SpritePhaser:在 Sprite 上附加触摸事件
【发布时间】:2014-10-25 16:37:18
【问题描述】:

我是移相器的新手,现在我使用移相器 v.2.0.7。我想要做的是让精灵处理触摸事件。

如何将onTap 附加到精灵对象?

我知道sprite_obj.events.onInputDown 可以实现触摸事件,但我仍然使用onInputUp,因为当onInputDown 之后触发弹出/模式(警报)时,您需要在此之后单击两次才能让事件监听器再次工作。 (My personal workaround for this issue is using inInputUp.)

我尝试的另一件事是在我的画布对象上添加onTap canvas.input.onTap.add,我认为这不适合实现我的目标。是的,它现在可以处理触摸事件,但问题是我想将触摸事件仅限于画布上的精灵图像,而不是整个画布。

谁能帮帮我。谢谢。

【问题讨论】:

    标签: touch phaser-framework


    【解决方案1】:

    您首先需要启用 Sprite 进行输入:

    sprite.inputEnabled = true;

    然后,您可以监听 Sprite 在输入时调度的任何事件,例如:

    sprite.events.onInputDown.add(onDown, this);
    
    ...
    
    function onDown(sprite, pointer) {
     // do something wonderful here
    }
    

    回调发送 2 个参数:导致输入事件的 Sprite 和指针(在多输入系统中,这可能经常变化)

    指针有很多你可以访问的属性,例如它被放下的时间、移动历史等。有关详细信息,请参阅Pointer docs

    Sprite 有很多事件,但这些是 Input 相关的(这是直接从 Phaser 源代码中提取的):

    /**
    * @property {Phaser.Signal} onInputOver - This signal is dispatched if the parent is inputEnabled and receives an over event from a Pointer.
    * @default null
    */
    this.onInputOver = null;
    
    /**
    * @property {Phaser.Signal} onInputOut - This signal is dispatched if the parent is inputEnabled and receives an out event from a Pointer.
    * @default null
    */
    this.onInputOut = null;
    
    /**
    * @property {Phaser.Signal} onInputDown - This signal is dispatched if the parent is inputEnabled and receives a down event from a Pointer.
    * @default null
    */
    this.onInputDown = null;
    
    /**
    * @property {Phaser.Signal} onInputUp - This signal is dispatched if the parent is inputEnabled and receives an up event from a Pointer.
    * @default null
    */
    this.onInputUp = null;
    
    /**
    * @property {Phaser.Signal} onDragStart - This signal is dispatched if the parent is inputEnabled and receives a drag start event from a Pointer.
    * @default null
    */
    this.onDragStart = null;
    
    /**
    * @property {Phaser.Signal} onDragStop - This signal is dispatched if the parent is inputEnabled and receives a drag stop event from a Pointer.
    * @default null
    */
    this.onDragStop = null;
    

    【讨论】:

    • 谢谢老兄。但我不能使用 onInputDown :(为什么?因为它有问题,如 alert() 和 bootstrap modal 之类的问题,在此处的问题中描述:html5gamedevs.com/topic/4355-click-on-an-image-not-working
    • 我真的不明白该 URL 中的问题与您所说的您正在尝试做的事情的相关性。 onInputDown 不是一个 DOM 事件,它是一个信号。如果你需要捕获 DOM 事件,你需要在 DOM 元素上捕获它们,它们不能被伪造。
    • 这种情况就像当我在画布上单击精灵图像并且出现问题时,我会以模态形式显示错误。这就是 onInputDown 的问题所在。所以我没有捕获 dom 事件。它只是在我依赖的移相器输入事件上。对不起,英语不好先生。谢谢
    • 当然焦点会回到你显示的DOM对象上。但这并不意味着当你关闭它时你不能再次将它设置回画布。
    • 如果我不清楚,您可以在关闭模式后执行game.canvas.focus() 将焦点重新设置回游戏。
    【解决方案2】:

    你试过吗? this.input.onDown.add(obj.method,obj);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多