【发布时间】: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