【问题标题】:libGDX bullet position logiclibGDX 子弹位置逻辑
【发布时间】:2015-02-15 20:08:14
【问题描述】:

我的游戏有一个更新方法来处理我的角色的火能力。 我的问题是我的游戏逻辑,子弹应该从我的角色位置发射,在游戏开始时(不移动角色)发射子弹来自角色的位置,但是当我移动我的角色时,子弹的开始位置和人物的位置不一样。

子弹的方向取决于玩家的方向。

private void update() {
    Vector2 direction = new Vector2(0, 0);


    if (Gdx.input.isKeyPressed(Keys.D)) {

        direction.x = 1f ;
    }
     if (Gdx.input.isKeyPressed(Keys.A)) {

        direction.x = -1f ;
    }
     if (Gdx.input.isKeyPressed(Keys.W)) {

        direction.y = 1f ;
    }
     if (Gdx.input.isKeyPressed(Keys.S)) {

        direction.y = -1f;
    }

    if (direction.x != 0 || direction.y != 0) {

        playerDirection.set(direction);
        System.out.println("player x: " +playerDirection.x + "\t" +"player y:"+playerDirection.y);
    }

    if (Gdx.input.isKeyPressed(Keys.F)) {
        bulletPos = new Vector2(startPos);
        bulletDirection.set(playerDirection);           
    }
    if (bulletPos != null) {
        bulletPos.x += direction.x;
        bulletPos.y +=direction.y;
        if (bulletPos.x < 0 || bulletPos.x > mapPixelWidth
                || bulletPos.y < 0 || bulletPos.y > mapPixelHeight) {
            bulletPos = null;

        }
    }


}

谁能知道逻辑错误,或者那里的任何人都可以提供向一个方向射击的简单逻辑?

【问题讨论】:

    标签: libgdx


    【解决方案1】:

    据我所知,您总是从玩家开始位置(startPos)触发按钮,然后,当子弹移动时,您根据玩家方向(direction.x,.y)更新其位置?但是当玩家改变方向时,子弹也开始向其他方向移动。错了。

    创建后,子弹应始终独立于播放器移动。所以当你创建它时,它必须有自己的起始位置(取自当前玩家位置)和自己的方向(基于当前玩家位置)。您的玩家移动无法再改变子弹位置。

    类似:

    if (Gdx.input.isKeyPressed(Keys.F)) {
    bulletPos.set(currentPlayerPosition);
    bulletDirection.set(currentPlayerDirection);
    }
    
    //and then in update()
    bulletPos.x+=bulletDirection.x;
    bulletPos.y+=bulletDirection.y;
    //until it goes out of screen, or passes given distance, 
    //or new bullet is created, or whatever condition you like
    

    【讨论】:

      猜你喜欢
      • 2018-09-13
      • 2012-09-12
      • 2012-02-01
      • 2016-12-03
      • 1970-01-01
      • 2012-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多