【问题标题】:How to make one character follow the main character path? LIBGDX / JAVA如何让一个角色跟随主角路径? LIBGDX / JAVA
【发布时间】:2020-09-02 09:36:06
【问题描述】:

我正在使用 Box2d 在 libgdx/java 中的 RPG 2d 中工作。

我有主角和其他 3 个跟随者。我希望他们像蛇一样跟随主角的同一条路线(与大师系统/梦幻之星等旧RPG相同),以免发现狭窄的入口时搞砸。

在我的 PlayScreen 类中,我单独调用所有角色。

    private static Alis _player;
    private static Myau _follower;
    private static Odin _follower1;
    private static Noah _follower2;

在构造函数播放屏幕上

_player = new Alis(world, this, mapManager);
_follower = new Myau(world, this, mapManager);
_follower1 = new Odin(world, this, mapManager);
_follower2 = new Noah(world, this, mapManager);

HandleInput(float dt) 方法我只移动主角

if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) && _player.b2body.getLinearVelocity().x <= 0.5f) {
_player.b2body.applyLinearImpulse(new Vector2(0.05f, 0), _player.b2body.getWorldCenter(), true);

}else if(Gdx.input.isKeyPressed(Input.Keys.LEFT) && _player.b2body.getLinearVelocity().x >= -0.5f) {
_player.b2body.applyLinearImpulse(new Vector2(-0.05f, 0), _player.b2body.getWorldCenter(), true);

}else if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) == Gdx.input.isKeyPressed(Input.Keys.LEFT)){
_player.b2body.setLinearVelocity(0f, _player.b2body.getLinearVelocity().y);
}

if(Gdx.input.isKeyPressed(Input.Keys.UP) && _player.b2body.getLinearVelocity().y <= 0.5f) {
_player.b2body.applyLinearImpulse(new Vector2(0, 0.05f), _player.b2body.getWorldCenter(), true);
} else if(Gdx.input.isKeyPressed(Input.Keys.DOWN) && _player.b2body.getLinearVelocity().y >= -0.5f) {
_player.b2body.applyLinearImpulse(new Vector2(0, -0.05f), _player.b2body.getWorldCenter(), true);
} else if(Gdx.input.isKeyPressed(Input.Keys.UP) == Gdx.input.isKeyPressed(Input.Keys.DOWN)){          _player.b2body.setLinearVelocity(_player.b2body.getLinearVelocity().x, 0f);
}

update(float dt) 方法

handleInput(dt);
_player.update(dt);
_follower.update(dt, _player.b2body.getPosition());
_follower1.update(dt, _follower.b2body.getPosition());
_follower2.update(dt, _follower1.b2body.getPosition());

render(float delta) 方法

game.batch.begin();
_follower2.draw(game.batch);
_follower1.draw(game.batch);
_follower.draw(game.batch);
_player.draw(game.batch);
game.batch.end();

dispose() 方法

_player.dispose();
_follower.dispose();
_follower1.dispose();
_follower2.dispose();

现在,在我的角色类中,使用 update(float dt, Vector2 position) 方法

if ((position.x - b2body.getPosition().x) > (15 /PhantasyStar.PPM)) {
            b2body.setLinearVelocity(new Vector2(0.5f, 0));

} else if ((position.x - b2body.getPosition().x) < ( - 15 /PhantasyStar.PPM)) {
            b2body.setLinearVelocity(new Vector2(-0.5f, 0));

} else if ((position.y - b2body.getPosition().y) > (15 / PhantasyStar.PPM)) {
            b2body.setLinearVelocity(new Vector2(0, 0.5f));

} else if ((position.y - b2body.getPosition().y) < (-15 / PhantasyStar.PPM)) {
            b2body.setLinearVelocity(new Vector2(0, -0.5f));

} else if (((position.y - b2body.getPosition().y) <= (15 / PhantasyStar.PPM) && (position.y - b2body.getPosition().y) >= (-15 / PhantasyStar.PPM)) || ((position.x - b2body.getPosition().x) <= (15 /PhantasyStar.PPM) && (position.x - b2body.getPosition().x) >= (-15 /PhantasyStar.PPM)) ) {
            b2body.setLinearVelocity(new Vector2(0, 0));
         }

setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 4);

setRegion(getFrame(dt));

这段代码给了我一个很好的结果,例如,如果我只在 x 轴或只在 y 轴上行走,玩家会完全按照我需要的方式行走,并且彼此之间保持正确的距离并正确返回,但是如果我开始上下移动,他们开始互相跟随,但每次我发现障碍物时,一些跟随者就会卡住。所以我希望他们能完全按照主角的路线走。

我 100% 我所做的事情是不正确的,但我试图为此找到一些文献但我没有找到。我相信如果我能找到一种方法来获得主要玩家的最后一个位置并使其成为第二个目标。

我尝试在将玩家位置更新为最后一个位置后收集并用于跟随者的下一次更新,但是,即使我要求跟随者跟随该位置,它也太接近了,所以我认为它是不是正确的收集位置,或者可能是另一种收集方式。我是否需要创建一个数组来收集所有位置并将此信息延迟传递给其他角色?

有没有在游戏开发方面更有经验的人来帮助我?

【问题讨论】:

  • 为了将来参考,rpg 标签是用于 RPG 语言的,而不是用于角色扮演游戏的。
  • 感谢 Federico,我将包含我的代码。
  • 就我而言,我比您的编辑更早投票决定关闭。现在我投票决定重新开放。如果另外两个用户也这样做,您的问题将被重新打开。
  • 看起来人们认为这不需要重新打开。如果您重新打开它并且有人注意到这个问题存在,您的新问题将再次关闭。您可以尝试删除这个并创建一个新的,但我不能保证您会成功。
  • 为何过早关闭?甚至德国的官僚机构也没有那么严格......

标签: java libgdx frameworks box2d


【解决方案1】:

在 PlayScreen 类上创建 2 个数组变量。这两个数组会收集主要玩家的位置和状态。

public class PlayScreen implements Screen {
private Array<Vector2> _playerPath;
private Array<Alis.State> _playerState;
}

在 PlayScreen 构造器上,我初始化了数组,所以在 .get 和 .set 之后就可以工作了。

public PlayScreen(PhantasyStar game, MapManager mapManager){

//starting Array for player path and state
_playerPath = new Array<Vector2>(100);
_playerState = new Array<Alis.State>(100);
for(int k=0;k<100;k++) {
_playerPath.add(new Vector2(0f, 0f));
_playerState.add(Alis.State.STANDING_DOWN);
}
//Create Player
_player = new Alis(world, this, mapManager);
_follower = new Myau(world, this, mapManager);
_follower1 = new Odin(world, this, mapManager);
_follower2 = new Noah(world, this, mapManager);
}

仍然在 PlayScreen 类上,更新方法。每个位置和每个状态都会在数组中更新,为追随者创建一条路径。我更新了关注者更新方法,每个延迟了 20 帧。

public void update(float dt){

//start ***Items for the following path***

 for (int i = 99; i > 0; i--) {
    _playerPath.get(i).set(_playerPath.get(i - 1));
    _playerState.set(i,_playerState.get(i - 1));
 }
    _playerPath.get(0).set(_player.b2body.getPosition());
    _playerState.set(0, _player.currentState);

handleInput(dt);

world.step(1/60f, 6, 2);

_player.update(dt);

_follower.update(dt, _playerPath.get(20), _playerState.get(20));

_follower1.update(dt, _playerPath.get(40), _playerState.get(40));

_follower2.update(dt, _playerPath.get(60), _playerState.get(60));
// end ***Items for the following path ***


        gamecam.position.x = _player.b2body.getPosition().x;
        gamecam.position.y = _player.b2body.getPosition().y;

//removing the blank spaces in the borders stopping the camera
xOffset = (gamecam.position.x - PhantasyStar.V_WIDTH / PhantasyStar.PPM / 2);
xOffsetRight = (mapManager.getMapWidth() / PhantasyStar.PPM) - gamecam.position.x - (PhantasyStar.V_WIDTH / PhantasyStar.PPM / 2);

yOffset = (gamecam.position.y - (PhantasyStar.V_HEIGHT / PhantasyStar.PPM / 2));
yOffsetUp = (mapManager.getMapHeight() / PhantasyStar.PPM) - gamecam.position.y - (PhantasyStar.V_HEIGHT / PhantasyStar.PPM / 2);

if(xOffset < 0)
gamecam.position.x = PhantasyStar.V_WIDTH / PhantasyStar.PPM / 2;
if(xOffsetRight < 0)
gamecam.position.x = (mapManager.getMapWidth() / PhantasyStar.PPM) - (PhantasyStar.V_WIDTH / PhantasyStar.PPM / 2);

if(yOffset < 0)
gamecam.position.y = PhantasyStar.V_HEIGHT / PhantasyStar.PPM / 2;
if(yOffsetUp < 0)
gamecam.position.y = (mapManager.getMapHeight() / PhantasyStar.PPM) - (PhantasyStar.V_HEIGHT / PhantasyStar.PPM / 2);       
// end removing blank spaces

//portal contact
 if(isCollided) {
   updateMap();
 }

 gamecam.update();
 renderer.setView(gamecam);
 }

下一个代码是 Follower 类,使用数组中的位置和状态。

public TextureRegion getFrame(float dt, Alis.State state) {

        currentState = getState(state);

        TextureRegion region;

        switch (currentState) {
            case RUN_UP:
                region = myauRun_Up.getKeyFrame(stateTimer, true);
                break;
            case RUN_DOWN:
                region = myauRun_Down.getKeyFrame(stateTimer, true);
                break;
            case RUN_RIGHT:
                region = myauRun_Right.getKeyFrame(stateTimer, true);
                break;
            case RUN_LEFT:
                region = myauRun_Left.getKeyFrame(stateTimer, true);
                break;
            case STANDING_UP:
                region = myauStand_Up;
                break;
            case STANDING_RIGHT:
                region = myauStand_Right;
                break;
            case STANDING_LEFT:
                region = myauStand_Left;
                break;

            case STANDING_DOWN:
            default:
                region = myauStand_Down;
                break;

        }
        stateTimer = currentState == previousState ? stateTimer + dt : 0;
        previousState = currentState;
        return region;
    }

    public State getState(Alis.State state) {

        if (state == Alis.State.RUN_UP)
            return State.RUN_UP;
        else if (state == Alis.State.RUN_DOWN)
            return State.RUN_DOWN;
        else if (state == Alis.State.RUN_RIGHT)
            return State.RUN_RIGHT;
        else if (state == Alis.State.RUN_LEFT)
            return State.RUN_LEFT;
        else if (state == Alis.State.STANDING_UP)
            return State.STANDING_UP;
        else if (state == Alis.State.STANDING_LEFT)
            return State.STANDING_LEFT;
        else if (state == Alis.State.STANDING_DOWN)
            return State.STANDING_DOWN;
        else
            return State.STANDING_DOWN;

    }

    public void update(float dt, Vector2 position, Alis.State state) {

        setPosition(position.x - getWidth() / 2, position.y - getHeight() / 4);

        setRegion(getFrame(dt, state));

    }

我希望这可以帮助其他人

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    相关资源
    最近更新 更多