简体   繁体   English

当我更改精灵的位置时,它会继续自行移动

[英]When I change the position of a sprite, it continues moving on its own

I'm trying to create a Snake-game using Phaser.js but I get stuck when it comes to moving the head around. 我正在尝试使用Phaser.js创建一个Snake游戏,但是在移动头部时我陷入了困境。

To make it easier on myself, I've opted to not use any velocity and handle movement by manually setting the coordinates of the sprite, my theory being that this will make it easier to handle movement once the snake grows and every segment needs to move to the same position the segment in front of it used to occupy. 为了使自己更轻松,我选择不使用任何速度并通过手动设置精灵的坐标来处理运动,我的理论是,一旦蛇长大并且每个部分都需要移动,这将使处理运动更容易移至其前面的路段以前所占据的位置。 I basically plan to treat my game-world as a grid, and that's not going to be very easy if everything has a velocity. 我基本上计划将我的游戏世界视为一个网格,如果一切都具有速度,那将不是一件容易的事。

I have an update function that should move my sprite every 50th time it's called. 我有一个update函数,该函数应在每次调用精灵50次后移动一次。

function update() {
    snake.body.velocity.x = 0;
    snake.body.velocity.y = 0;

    if (cursors.up.isDown && direction !== 'down') {
        direction = 'up';
    } else if (cursors.down.isDown && direction !== 'up') {
        direction = 'down';
    } else if (cursors.left.isDown && direction !== 'right') {
        direction = 'left';
    } else if (cursors.right.isDown && direction !== 'left') {
        direction = 'right';
    }

    if (updateCount % 50 === 0) {
        moveSnake();
    }
    updateCount += 1;
}

function moveSnake() {
    switch(direction) {
        case 'up': snake.y -= 2; break;
        case 'down': snake.y += 2; break;
        case 'left': snake.x -= 2; break;
        case 'right': snake.x += 2; break;
    }
}

But this seems to trigger som kind of velocity even though I'm explicitly setting it to 0. The snake (let's pretend it looks like one) will keep moving 2 pixels at a time until the next time moveSnake gets called, at which point it will pick up the pace and move 4 pixels at the time untill it hits the edge of the gaming area. 但这似乎触发了某种速度,即使我将其显式设置为0也是如此。蛇(假设它看起来像一个)将一次​​一直移动2个像素,直到下次调用moveSnake为止,此时它会加快速度并一次移动4个像素,直到到达游戏区域的边缘。

I've created the sprite in the create function and enabled the arcade physics engine like this: 我已经在create函数中创建了精灵,并启用了街机物理引擎,如下所示:

game.physics.startSystem(Phaser.Physics.ARCADE);
game.world.setBounds(0, 0, 800, 600);

snake = game.add.sprite(400, 300, 'snake-body');
game.physics.enable(snake, Phaser.Physics.ARCADE);
snake.body.collideWorldBounds = true;

I've verified that the throttling in update works and that moveSnake does not get called on every game-loop. 我已经验证了限制update工作原理,并且在每个游戏循环中都未调用moveSnake

Full source at github (but there's honestly not much more there than what I've just pasted here) github上的完整源代码(但坦白讲,这里没有比我刚刚粘贴的内容多的东西了)

EDIT 编辑

To clear up a bit of confusion, in my code moveSnake only gets invoked 3 times (try adding a breakpoint) and I expected the sprite to have moved only 6 pixels over these 3 invocations, but instead it moves 400 pixels. 为了消除混乱,在我的代码中moveSnake仅被调用3次(尝试添加断点),并且我希望精灵在这3次调用中仅移动了6个像素,但相反却移动了400个像素。 My question was why the x-coordinate of the sprite changes on its own and not only when I explicitly set it. 我的问题是,为什么精灵的x坐标会自动改变,而不仅是在我明确设置它的时候。

Don't set the x and y yourself, let the physics handle it, by giving body, the according velocity. 不要自己设置x和y,让物理学家通过赋予身体相应的速度来处理它。

function moveSnake() {
    snake.body.velocity.x = 0;
    snake.body.velocity.y = 0;

    switch(direction) {
        case 'up': snake.body.velocity.y = -2; break;
        case 'down': snake.body.velocity.y = 2; break;
        case 'left': snake.body.velocity.x = -2; break;
        case 'right': snake.body.velocity.x = 2; break;
    }
}

Also you can ask your questions on Phaser forums . 您也可以在Phaser论坛上提问。

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

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