简体   繁体   English

如何使精灵跳起并返回到原始位置GDXLib

[英]How to make a sprite jump up and return to original position GDXLib

I'm very new to programming so bear with me here... 我对编程很新,所以在这里忍受我...

I'm making a basic 2d android game. 我正在做一个基本的2d安卓游戏。 I am trying to simply make a sprite jump up in the air a bit and then land back to where it originally started. 我试图简单地让一个精灵跳起来,然后降落到最初开始的地方。 The sprite that I want to jump is "ball2". 我要跳的精灵是“ball2”。 This is my code so far (Basically just added textures and the sprite in the correct position to start the jump but haven't done anything else). 这是我的代码到目前为止(基本上只是添加纹理和精灵在正确的位置开始跳转但没有做任何其他事情)。

Any help is appreciated. 任何帮助表示赞赏。

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


@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);

    spike1 = new Texture("spike1.png");
    spike1.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
    spike2 = new Texture("spike2.png");

}

@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 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();

    Gdx.input.setInputProcessor(new InputAdapter () {
            @Override
               public boolean keyDown (int keycode) {
                if(keycode==Keys.UP)
                {
                   ball.setHeight(ball.getHeight()+50);
                }
                return true;
               }  
    }


}

Then you need to add the timer down down or you could add down button. 然后你需要向下添加计时器,或者你可以添加按钮。 Not sure if you have spikes on the top as well. 不确定你是否也有尖峰。 I don't know what it's supposed to look like. 我不知道它应该是什么样子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM