【发布时间】:2018-09-06 19:43:52
【问题描述】:
我正在开发一个角色能够跳跃的游戏。他可以使用 Vector2 完美地沿 x 轴移动,但只要我添加 y 分量(以便能够向上移动),我的精灵就会消失。我以这种方式实现他的 x 轴运动:
position.add(200 * dt, velocity.y * dt);
其中 200 是速度,dt 是自上一帧渲染以来经过的时间。
为了向上移动,我有一个名为“jump”的方法,它是这样实现的:
public void jump () {
velocity.y = 20;
}
因此,在我的 PlayScreen 中,我调用了 jump 并且角色应该向上移动,但相反,它消失了。我对游戏中的其他对象进行了尝试,它们的行为方式都相同——只要我尝试向上或向下移动它们,它们就会消失。我真的不明白问题出在哪里。
P.S 我的世界尺寸为 136 像素(宽度)* 设备的屏幕高度
更新更多信息: 这是我的整个角色类:
public class Astroboy{
public Vector2 velocity;
public Vector2 position;
public Animation<TextureRegion> heroAnim;
private TextureAtlas atlas;
public Rectangle astroRect;
public Astroboy (float x, float y){
velocity = new Vector2(200, 0);
position = new Vector2(x, y);
atlas = new TextureAtlas("Anims/Hero_Anim.atlas");
heroAnim = new Animation<TextureRegion>(0.15f, atlas.findRegions("HeroRunning") , Animation.PlayMode.LOOP); // animation
}
public void update (float dt) {
position.add(velocity.x * dt, velocity.y * dt);
}
public void jump () {
velocity.y = 10;
}
public Animation<TextureRegion> getAnimation () {
return heroAnim;
}
}
SpriteBatch 正在 PlayState 屏幕中使用用于绘制移动对象的相机绘制角色:
//SETTING CAMERAS
fixedCamera1 = new OrthographicCamera(); // camera for static background
fixedCamera1.setToOrtho(false, WORLD_WIDTH, WORLD_HEIGHT);
stageCamera1 = new OrthographicCamera(); // camera for a character in motion
stageCamera1.setToOrtho(false, WORLD_WIDTH, WORLD_HEIGHT);
//--------------------
batch.setProjectionMatrix(stageCamera1.combined);
batch.begin();
batch.draw(astroboy.getAnimation().getKeyFrame(stateTime, true), astroboy.position.x, astroboy.position.y, WORLD_WIDTH / 4, WORLD_HEIGHT / 4);
batch.end
而跳跃动作仅用
实现if (Gdx.input.justTouched()) {
astroboy.jump();
}
【问题讨论】:
-
你能分享更多包含位置更新的循环的重要部分吗?仅从您共享的代码很难猜出发生了什么。
-
@CapnSparrow,我用更多信息更新了问题
-
WORLD_WIDTH和WORLD_HEIGHT是整数还是浮点数?你能显示你在哪里设置stageCamera1 -
@Morchul,他们都是浮动的,我编辑了问题
-
看起来不错。我找不到一些问题。尝试调试astroboy的位置和速度。如果你有你的项目在 github 上或者我可以帮你调试。