简体   繁体   English

Phaser Box2d-沿一个方向锁定拖动

[英]Phaser Box2d - Lock drag in one direction

I am working on Phaser's Box2d plugin to build a game. 我正在使用Phaser的Box2d插件来构建游戏。 In the game, objects can be dragged around with mouse. 在游戏中,可以用鼠标拖动对象。 But I want to fix the drag direction ie object should move only horizontal or vertical direction. 但是我想固定拖动方向,即对象只能水平或垂直移动。

I have checked official examples and docs. 我检查了官方示例和文档。 Couldn't find anything which serves the purpose. 找不到符合目的的任何东西。

This example shows the motion direction lock using sprite.input.allowVerticalDrag = false , but it is doesn't work with Box2d's drag. 此示例显示了使用sprite.input.allowVerticalDrag = false的运动方向锁定,但不适用于Box2d的拖动。

I am following this example to enable the drag. 我正在按照此示例启用拖动。 I have tried setting sprite.body.y to a fix value like 300 in both mouseDragMove and update functions, so that it does move in y direction. 我曾尝试设置sprite.body.y像两个300一固定值mouseDragMoveupdate功能,因此,它并在移动y方向。 But results are not smooth. 但是结果并不顺利。 It still shakes a little bit in that direction. 它仍然朝那个方向晃动了一点。

What can I do implement this? 我该怎么做呢? Am I missing any built-in option of the plugin? 我是否缺少该插件的任何内置选项?

I was able to figure out a solution for it. 我能够找到解决方案。 What I did is to override the sprite position, in a particular axis, in mousePointer parameter that is passed to mouseDragMove handler of the framework. 我所做的是在传递给框架的mouseDragMove处理程序的mousePointer参数中重写特定轴上的精灵位置。

Here is how it works - 下面是它的工作原理 -

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;
}

We had a similar issue on a game we were making and although our game doesn't use physics, what we made may be useful to you. 我们在制作的游戏中也遇到了类似的问题,尽管我们的游​​戏不使用物理原理,但我们制作的内容可能对您有用。 The whole idea is to use tweens to set an object's position while dragging it (as opposed to setting its position directly) - this way you can run your checks and set your constrains manually while the tween is executing and if your input is where your object shouldn't be, you simply do not perform a tween to that position. 整个想法是在拖动对象时使用补间设置对象的位置(而不是直接设置其位置)-这样,您可以在执行补间以及输入是对象所在的位置时运行检查并手动设置约束不应该,您只是不执行补间到该位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM