我正在开发相位器Box2d插件来构建一个游戏。在游戏中,物体可以用鼠标拖动。但我想要固定拖动方向,即物体应该只向水平或垂直方向移动。
我查过官方的例子和文档。找不到任何有用的东西。
这个例子使用sprite.input.allowVerticalDrag = false显示运动方向锁,但它不适用于Box2d的拖动。
我正在跟踪这个例子以启用拖动。我尝试将sprite.body.y设置为mouseDragMove和update函数中类似于300的fix值,这样它就会朝着y方向移动。但结果并不顺利。它在那个方向上仍然有一点震动。
我能做些什么来实现这一点?我错过了任何内置的插件选项吗?
发布于 2015-12-21 06:53:29
我想出了解决办法。我所做的是覆盖在特定轴上传递给框架的mousePointer处理程序的mouseDragMove参数中的sprite位置。
以下是它的工作原理-
var isDragging = false,
activeSpriteX=0,
activeSpriteY=0;
function mouseDragStart(e) {
isDragging = true;
//get the clicked sprite
var currentSprite = game.physics.box2d.getBodiesAtPoint(e.x, e.y);
//save the position of clicked sprite
if (currentSprite.length > 0) {
activeSpriteX = game.input.mousePointer.x;
activeSpriteY = game.input.mousePointer.y;
}
game.physics.box2d.mouseDragStart(game.input.mousePointer);
}
function mouseDragMove() {
mousePointer = game.input.mousePointer;
//if sprite is being dragged
if (isDragging) {
//HERE IS THE WHOLE TRICK -
//just override the vertical position of `mousePointer` to sprite's initial position, when it was clicked
//To fix the sprite in horizontal direction, just override the `x`
mousePointer.y = activeCarY;
}
game.physics.box2d.mouseDragMove(mousePointer);
}
function mouseDragEnd(e) {
game.physics.box2d.mouseDragEnd();
isDragging = false;
}发布于 2015-12-13 16:34:29
我们制作的游戏也有类似的问题,虽然我们的游戏不使用物理,但我们所做的可能对你有用。整个想法是在拖动对象时使用tweens来设置对象的位置(而不是直接设置它的位置)--通过这种方式,您可以在吐温执行时运行检查并手动设置约束,如果您的输入位于对象不应该在的位置,则根本不执行对该位置的检查。
https://stackoverflow.com/questions/34237597
复制相似问题