【问题标题】:How to make character continue moving after releasing key, in Slick2D?在 Slick2D 中,如何使角色在释放键后继续移动?
【发布时间】:2016-08-07 18:38:00
【问题描述】:

我是 Slick2D 的新手,我正在尝试我的角色的动作。我可以在按住移动键的同时使其平稳移动,但我也希望角色完成其移动,以便它准确地停在下一个瓷砖上(我有简单的地图,瓷砖 32x32)。这对我来说是个问题,因为它会移动到下一个图块,但它会传送到那里 - 移动是即时的,我希望我的角色以相同的速度继续移动。

例如,我在 update() 方法中尝试过类似的方法:

else if (input.isKeyPressed(Input.KEY_D))
    {
        characterAnimation = characterAnimationRight;
        characterAnimation.update(delta);
        xCoord = (int) xCoord;
        while (xCoord%32 != 0)
        {
            xCoord += 1;
            characterAnimation.update(delta);
            if (xCoord > Window.WIDTH - 32)
            {
                xCoord = Window.WIDTH - 32;
            }

        }
    }

但我不能让它工作。

【问题讨论】:

    标签: java slick2d


    【解决方案1】:

    您为什么不尝试设置“xSpeed”和“ySpeed”,并根据“characterAnimation”的设置位置以及您是否位于确切的图块上来设置这些值?

    类似:

    else if (input.isKeyPressed(Input.KEY_D)) {
        characterAnimation = characterAnimationRight;
    }
    
    // ...
    
    if (characterAnimation == characterAnimationRight){
        xSpeed = 1;
    }
    else if (characterAnimation == characterAnimationLeft){
        xSpeed = -1;
    }
    xCoord += xSpeed;
    characterAnimation.update(delta);
    if(xCoord % 32 == 0) {
        xSpeed = 0;
    }
    // ...
    

    我实际上并不知道您的代码(或 slick2d)是如何工作的,所以我假设在调用 characterAnimation.update(delta) 时会神奇地考虑到 xCoord 值。否则根据xCoord 做任何你需要更新角色位置的事情。

    【讨论】:

      【解决方案2】:

      解决方法是在update()方法中不计算while()中的xCoord。原因是它是在update() 方法的单次运行中计算出来的,然后调用render() 方法来渲染字符。这意味着角色会在 while() 之后呈现,然后传送。

      这是我的解决方案:

      @Override
      public void update(GameContainer gc, StateBasedGame s, int delta)
              throws SlickException {
      
          // .......
      
          if (moving)
          {
              if (movingDirection == DIR_RIGHT)
              {
                  if (xCoord >= targetCoord)
                  {
                      xCoord = targetCoord;
                      moving = false;
                  }
                  else
                  {
                      xCoord += delta * 0.1f;
                      characterAnimation.update(delta);
                      if (xCoord > Window.WIDTH - 32)
                      {
                          xCoord = Window.WIDTH - 32;
                      }
                  }
              }
              else if (movingDirection == DIR_LEFT)
              {
                  if (xCoord <= targetCoord)
                  {
                      xCoord = targetCoord;
                      moving = false;
                  }
                  else
                  {
                      xCoord -= delta * 0.1f;
                      characterAnimation.update(delta);
                      if (xCoord < 0)
                      {
                          xCoord = 0;
                      }
                  }
              }
              else if (movingDirection == DIR_UP)
              {
                  if (yCoord <= targetCoord)
                  {
                      yCoord = targetCoord;
                      moving = false;
                  }
                  else
                  {
                      yCoord -= delta * 0.1f;
                      characterAnimation.update(delta);
                      if (yCoord < 0)
                      {
                          yCoord = 0;
                      }
                  }
              }
              else if (movingDirection == DIR_DOWN)
              {
                  if (yCoord >= targetCoord)
                  {
                      yCoord = targetCoord;
                      moving = false;
                  }
                  else
                  {
                      yCoord += delta * 0.1f;
                      characterAnimation.update(delta);
                      if (yCoord > Window.WIDTH - 32)
                      {
                          yCoord = Window.WIDTH - 32;
                      }
                  }
              }
          }
      }
      

      变量moving在按下移动键后设置为真。现在,在每次调用render() 之后,角色都会在update() 中移动一点,并在这个新位置进行渲染,直到它正好在图块上。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-08
        • 2020-03-06
        • 1970-01-01
        • 1970-01-01
        • 2013-11-16
        • 1970-01-01
        • 1970-01-01
        • 2017-10-09
        相关资源
        最近更新 更多