单击处理程序到单个独立的 Sprite

click handler to an individual isolated Sprite

有没有办法将点击处理程序绑定到单独的独立 Sprite

 sprite.getChildren().forEach(
        s => s.setInteractive({ cursor: 'url(./src/games/gewgly/assets/images/pointer.png), pointer' })
    );

我用这个方法绑定了孤立的精灵,但是还没成功,有没有更好的方法

Phaser 3 中的 Sprite 不能有 children,因此您的代码的 sprite.getChildren 部分让我有点担心。只有群组有 getChildren 方法。

最简单的:

const player = this.add.sprite(x, y, texture);

player.setInteractive();

player.on('pointerdown', () => {

  console.log('You clicked the player sprite');

});

// Or, if you have a Group full of Sprites:

const sprites = yourGroup.getChildren();

sprites.forEach((child) => {

  child.setInteractive();

  child.on('pointerdown', () => {
    console.log('You clicked a Group sprite');
  });

});