簡體   English   中英

android java libgdx 如何讓“紋理”跳起來並返回到起始位置

[英]How to make a 'texture' jump up and return to starting position android java libgdx

所以我只是想讓我的游戲角色,它是一個紋理( ball ),在空中跳躍,然后返回到按下屏幕時它開始的位置。 我只是想知道是否有人可以給我一個代碼示例或幫助我使用下面的當前代碼來做到這一點。 我基本上只是繪制了背景和球的紋理,並將球定位在我希望它開始跳躍的位置。 球的紋理是我想要直接跳起來的。

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture background;
    Texture ball;

    @Override
    public void create () {
        batch = new SpriteBatch();
        background = new Texture("gamebackground.png");

        ball = new Texture("ball2.png");
        ball.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
    }

    @Override
    public void render () {
        batch.begin();
        float scaleFactor = 2.0f;
        batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.draw(ball, 80, 145, ball.getWidth() * scaleFactor, ball.getHeight() * scaleFactor);
        batch.end();
    }

    @Override
    public void dispose () {}
}

有一百萬種方法可以做到這一點。

這是一個簡單的(而且不是很靈活的方式)。 創建一個 Ball 類,其中包含 x 和 y 位置、速度和加速度的變量。 然后給它一個將加速度和速度應用到位置的更新方法:

public class Ball {

    public static final float GRAVITY = -100; // size depends on your world scale
    public static final float BOUNCE_DAMPENING = 0.6f;

    public final Vector2 position = new Vector2();
    public final Vector2 velocity = new Vector2();
    public final Vector2 acceleration = new Vector2(0, GRAVITY);

    public void update (){
        float dt = Gdx.graphics.getDeltaTime();
        velocity.add(acceleration.x * dt, acceleration.y * dt));
        position.add(velocity.x * dt, velocity.y * dt);

        if (position.y <= 0){ // hit ground, so bounce
            position.y = -position.y * BOUNCE_DAMPENING;
            velocity.y = -velocity.y * BOUNCE_DAMPENING;
        }
    }

}

這是處理物理學的一種非常基本的方法。 使用 Box2D 會更復雜,但如果您只是學習,上面的內容就可以了。

現在,您需要創建一個球實例並使用它來跟蹤您的球位置。 繪制時使用 Ball 對象的位置。 您可以對點擊做出反應以應用速度。

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture background;
    Texture ballTexture;
    Ball ball;

    @Override
    public void create () {
        batch = new SpriteBatch();
        background = new Texture("gamebackground.png");

        ballTexture = new Texture("ball2.png");
        ballTexture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);

        ball = new Ball();
    }

    @Override
    public void render () {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // don't forget to clear screen

        if (Gdx.input.justTouched())
            ball.velocity.y += 100;
        ball.update();

        batch.begin();
        float scaleFactor = 2.0f;
        batch.draw(background, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.draw(ballTexture, ball.position.x, ball.position.y, ballTexture.getWidth() * scaleFactor, ballTexture.getHeight() * scaleFactor);
        batch.end();
    }

    @Override
    public void dispose () {
        batch.dispose();
        background.dispose();
        ballTexture.dispose();
    }
}

您還需要閱讀像素單位與世界單位以及如何使用視口解決比例問題。 https://xoppa.github.io/blog/pixels/https://github.com/libgdx/libgdx/wiki/Viewports

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM