如何使用移相器找到鼠标位置 x/y
how to find the mouse position x/y using phaser
我有问题,试图在他们点击图像时显示鼠标位置 x/y,我使用的是 Phaser 提供的点击和图像示例之一。
这是代码
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 + "!";
}
如果你想要输入的 x 和 y 位置
game.input.x;
game.input.y;
如果你特别想要鼠标
game.input.mousePointer.x;
game.input.mousePointer.y;
监听器函数会像
function listener () {
counter++;
text.text = game.input.mousePointer.x +"/"+game.input.mousePointer.y + counter + "!";
}
只是补充说 listener
函数将发送 2 个参数:精灵和指针。所以你可以这样做:
function listener (sprite, pointer) {
var x = pointer.x;
var y = pointer.y;
...
}
这将是最准确的方法,因为它考虑了多点触控设备,而访问输入。x/y 直接没有,它只包含最近的触摸事件坐标(在只有鼠标的环境中很好,但在其他任何地方都不行)。
我有问题,试图在他们点击图像时显示鼠标位置 x/y,我使用的是 Phaser 提供的点击和图像示例之一。
这是代码
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 + "!";
}
如果你想要输入的 x 和 y 位置
game.input.x;
game.input.y;
如果你特别想要鼠标
game.input.mousePointer.x;
game.input.mousePointer.y;
监听器函数会像
function listener () {
counter++;
text.text = game.input.mousePointer.x +"/"+game.input.mousePointer.y + counter + "!";
}
只是补充说 listener
函数将发送 2 个参数:精灵和指针。所以你可以这样做:
function listener (sprite, pointer) {
var x = pointer.x;
var y = pointer.y;
...
}
这将是最准确的方法,因为它考虑了多点触控设备,而访问输入。x/y 直接没有,它只包含最近的触摸事件坐标(在只有鼠标的环境中很好,但在其他任何地方都不行)。