如何使用相位器查找鼠标位置 x/y
how to find the mouse position x/y using phaser
我遇到了问题,当他们单击图像时尝试显示鼠标位置 x/y,我使用相位器提供的单击和图像示例之一。
这是代码
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 位置 o f 输入
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;
...
}
这将是最准确的使用方法,因为它考虑了多点触控设备,而直接访问input.x/y不会,它只包含最新的触摸事件坐标(在仅鼠标环境中很好,但在其他任何地方都不能)。
相关文章:
- 跟踪jqplot垂直折线图的鼠标位置
- Javascript-在视频中跟踪鼠标位置
- 打开相对于鼠标位置的CSS3/HTML5模式对话框
- 如何查明鼠标按下事件是否发生在滚动条上或元素中的其他任何位置
- 如何在鼠标悬停时将对象从起始位置移动到结束位置,然后在鼠标悬停后再次移动
- 如何控制quadraticCurve使用鼠标位置控制位置
- 从鼠标点(及更多)查询位置元素
- 在鼠标上触发鼠标移动'的当前位置
- Javascript如何在不使用画布的情况下获取鼠标位置
- jQuery根据鼠标位置计算DIV偏移量和边界
- 在火狐浏览器的画布上获取鼠标位置
- 尝试绘制从鼠标位置工具栏中选择的形状 - HTML5 CANVAS
- Kinectjs在画布上鼠标向下位置的准确性和clearRect未按预期工作
- Three.js-查找鼠标最后位置和当前位置之间的所有交点
- 在鼠标滚动上将页面内容滚动到自定义位置
- 添加视图框时更改鼠标位置
- Javascript/Canvas-查找相对于坐标网格位置的鼠标坐标
- 如何在鼠标位置获取所有元素
- 鼠标在HTML 5响应画布中的位置
- 查找事件的位置(鼠标悬停在按钮上),用于Firefox扩展