我正在尝试将视口坐标转换为全局(图层)坐标。
我试过像docs中描述的那样,但似乎不起作用:
var result= me.game.viewport.localToWorld(x,y, me.game.viewport.AXIS_BOTH);
我有,反之亦然组合(将全球转换为本地):
me.event.subscribe("pointerdown", function (event) {
// convert the given into local (viewport) relative coordinates
var pos = me.input.globalToLocal(event.clientX, event.clientY);
});
为什么我认为me.game.viewport.localToWorld不起作用? 因为对于我的实体,我在点击时有很大的坐标差异(比如超过200点)。
代码段:
game.PlayerEntity = me.Entity.extend({
/**
* constructor
*/
init:function (x, y, settings)
{
var self=this;
self.leftMouseClicked=false;
me.event.subscribe("pointerdown", function (event) {
if(!self.leftMouseClicked) {
var result= me.game.viewport.localToWorld(self.pos.x,self.pos.y, me.game.viewport.AXIS_BOTH);
console.log(result);
console.log(event.clientX);
console.log(event.clientY);
self.leftMouseClicked=true;
}
});
...
所以我的问题是:
如何将视口坐标转换为第一层,就像我们将全局转换为本地一样?
答案 0 :(得分:0)
我找到了解决方案,它已从 me.game.viewport.pos 中抵消。
结果代码:
var self=this;
me.event.subscribe("pointerdown", function (event) {
//me.game.viewport.pos -- offset
var pos = me.input.globalToLocal(event.clientX, event.clientY);
self.mouseXCoord = pos.x+me.game.viewport.pos.x;
self.mouseYCoord = pos.y+me.game.viewport.pos.y;
});