【问题标题】:Any sprite disappears when trying to move along y-axis尝试沿 y 轴移动时,任何精灵都会消失
【发布时间】: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_WIDTHWORLD_HEIGHT 是整数还是浮点数?你能显示你在哪里设置stageCamera1
  • @Morchul,他们都是浮动的,我编辑了问题
  • 看起来不错。我找不到一些问题。尝试调试astroboy的位置和速度。如果你有你的项目在 github 上或者我可以帮你调试。

标签: java libgdx


【解决方案1】:

猜想,dt 比你想象的要高,并且乘法将精灵推到了边界之外。首先 - 使用 x 和 y 变量,并为它们分配值 200 * dt 和 velocity.y * dt,这样你就可以看到发生了什么。

int x = 200 * dt;
int y = velocity.y * dt;
System.out.println("x="+x+", y="+y);
position.add(x, y);

编辑

libgdx 中的 Screen#render 定义如下:

 /** Called when the screen should render itself.
  * @param delta The time in seconds since the last render. */
 public void render (float delta);

所以传入dt的参数不太可能真的很高。

最初的建议仍然有效 - 在调试器中单步执行您的代码或打印出值,以便您了解发生了什么。

【讨论】:

  • 是的,我尝试这样做,但问题仍然不明显。我在跳跃过程中跟踪了 y 值,它刚刚开始增加(如假设的那样),坐标没有什么奇怪的。问题是,以前当我的游戏世界有尺寸设备的宽度 * 设备的高度而不是像现在这样的静态值时,一切正常,角色可以跳跃和跌倒
猜你喜欢
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 2013-01-12
相关资源
最近更新 更多