<!DOCTYPE HTML>
<html>
<head>
<!-- Shamelessly adapted from pixi.js Example 8 - Dragging -->
<title>Sprite to Mouse Snapping issue</title>
<script src="pixi.js"></script>
</head>
<body>
<script>
var stage = new PIXI.Stage(0x97c56e, true);
var renderer = PIXI.autoDetectRenderer(window.innerWidth, window.innerHeight, null);
// add the renderer view element to the DOM
document.body.appendChild(renderer.view);
renderer.view.style.position = "absolute";
renderer.view.style.top = "0px";
renderer.view.style.left = "0px";
requestAnimFrame( animate );
// create a texture from an image path
var texture = PIXI.Texture.fromImage("aboriginal-bunny.png");
createAboriginalBunny(100, 100);
function createAboriginalBunny(x, y)
{
var bunny = new PIXI.Sprite(texture);
bunny.setInteractive(true);
// leaving anchor point at 10% to illustrate the problem clearer
bunny.anchor.x = 0.1;
bunny.anchor.y = 0.1;
/*
* Set-up dragging
*/
bunny.mousedown = bunny.touchstart = function(data)
{
this.data = data;
this.alpha = 0.5;
this.dragging = true;
};
bunny.mouseup = bunny.mouseupoutside = bunny.touchend = bunny.touchendoutside = function(data)
{
this.alpha = 1
this.dragging = false;
// set the interaction data to null
this.data = null;
};
bunny.mousemove = bunny.touchmove = function(data)
{
if(this.dragging)
{
// need to get parent coords..
var newPosition = this.data.getLocalPosition(this.parent);
this.position.x = newPosition.x;
this.position.y = newPosition.y;
}
}
// move the sprite to initial pos
bunny.position.x = x;
bunny.position.y = y;
stage.addChild(bunny);
}
function animate() {
requestAnimFrame( animate );
renderer.render(stage);
}
</script>
</body>
</html>
首先,感谢您提供详细的错误报告和示例代码,当我们提供这么多信息时,它很有帮助!
不幸的是,这不是错误。 这个问题是由于您将精灵的位置设置为每次鼠标移动的鼠标位置(很可能是因为示例就是这样做的)。 真的,应该发生的是你应该通过鼠标移动的增量来更新你的精灵。 尝试这样的事情:
/*
* Set-up dragging
*/
bunny.mousedown = bunny.touchstart = function(data)
{
this.data = data;
this.alpha = 0.5;
this.dragging = this.data.getLocalPosition(this.parent);;
};
bunny.mouseup = bunny.mouseupoutside = bunny.touchend = bunny.touchendoutside = function(data)
{
this.alpha = 1
this.dragging = false;
// set the interaction data to null
this.data = null;
};
bunny.mousemove = bunny.touchmove = function(data)
{
if(this.dragging)
{
// need to get parent coords..
var newPosition = this.data.getLocalPosition(this.parent);
this.position.x += (newPosition.x - this.dragging.x);
this.position.y += (newPosition.y - this.dragging.y);
this.dragging = newPosition;
}
}
重要的位在mousedown
我存储鼠标的位置,然后在mousemove
我根据鼠标移动了多少来更新我的精灵位置(未将其设置为新位置),然后我下次更新存储的鼠标位置。 我希望这有帮助!
你好,
这解决了我的问题,并且更容易理解,谢谢。 是的,我的代码基于示例的工作方式,所以如果可以更新示例会很好——我记得在理解它的工作原理时遇到了麻烦,猜测它没有完全工作。 :P
一个相关的问题,是否严格需要在 mouseup 时将交互数据设置为 null?
我也不清楚什么时候需要 getLocalPosition,而不是当前位置。
附注。 不用担心:错误报告详细信息,至少我可以做到。 感谢提供图书馆。 :)
getLocalPosition
是将鼠标事件的绝对坐标转换为 DisplayObjects 使用的局部相对坐标。 每个 DisplayObject 的位置都相对于其父级,因此我们采用绝对鼠标坐标并使用this.data.getLocalPosition(this.parent)
将它们转换为相对于父级。
在 mouseup 上设置this.data = null
并不是特别必要。 实际上,您应该能够对每个函数使用data
参数,而根本不使用this.data
。
再次感谢你的帮助; 图形库非常新,因此要了解各种实现选择。 感谢 pixi 开发人员花时间及时回答我的问题。
由于关闭后没有任何近期活动,因此该线程已自动锁定。 请为相关错误打开一个新问题。
最有用的评论
首先,感谢您提供详细的错误报告和示例代码,当我们提供这么多信息时,它很有帮助!
不幸的是,这不是错误。 这个问题是由于您将精灵的位置设置为每次鼠标移动的鼠标位置(很可能是因为示例就是这样做的)。 真的,应该发生的是你应该通过鼠标移动的增量来更新你的精灵。 尝试这样的事情:
重要的位在
mousedown
我存储鼠标的位置,然后在mousemove
我根据鼠标移动了多少来更新我的精灵位置(未将其设置为新位置),然后我下次更新存储的鼠标位置。 我希望这有帮助!