如何使用移相器找到鼠标位置x / y

Ped*_*ro 3 javascript phaser-framework

我遇到了问题,尝试在鼠标单击图像时显示鼠标位置x / y,im使用了相位器提供的单击和图像示例之一。

这是代码

var game = new Phaser.Game(800, 500, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });

var text;
var counter = 0;

function preload () {

    //  You can fill the preloader with as many assets as your game requires

    //  Here we are loading an image. The first parameter is the unique
    //  string by which we'll identify the image later in our code.

    //  The second parameter is the URL of the image (relative)
    game.load.image('Happy-face', 'happy.png'); 

}

function create() {

    //  This creates a simple sprite that is using our loaded image and
    //  displays it on-screen and assign it to a variable
    var image = game.add.sprite(game.world.centerX, game.world.centerY, 'Happy-face');

    //  Moves the image anchor to the middle, so it centers inside the game properly
    image.anchor.set(0.5);

    //  Enables all kind of input actions on this image (click, etc)
    image.inputEnabled = true;
    this.position = new Phaser.Point();
    text = game.add.text(250, 16, '', { fill: '#ffffff' });

    image.events.onInputDown.add(listener, this);

}

function listener () {

    counter++;
    text.text = "Position x/y " + counter + "!";


}
Run Code Online (Sandbox Code Playgroud)

Sky*_*ker 5

如果要输入的x和y位置

game.input.x;
game.input.y;
Run Code Online (Sandbox Code Playgroud)

如果您想专门使用鼠标

game.input.mousePointer.x;
game.input.mousePointer.y;
Run Code Online (Sandbox Code Playgroud)

监听器的功能会像

function listener () {

counter++;
text.text = game.input.mousePointer.x +"/"+game.input.mousePointer.y +     counter + "!";


}
Run Code Online (Sandbox Code Playgroud)