【问题标题】:How to make a sprite jump up and return to original position GDXLib如何让精灵跳起来回到原来的位置 GDXLib
【发布时间】:2017-02-03 14:28:43
【问题描述】:

我对编程很陌生,所以请耐心等待......

我正在制作一个基本的 2d 安卓游戏。我试图简单地让一个精灵在空中跳跃一下,然后回到它最初开始的地方。我想跳的精灵是“ball2”。这是我到目前为止的代码(基本上只是在正确的位置添加了纹理和精灵以开始跳跃,但没有做任何其他事情)。

感谢任何帮助。

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

}

【问题讨论】:

  • 那么,flappybird 基本上是?您可以为点击设置一个侦听器,当他们点击时会在短时间内增加高度,并以每秒恒定的速率设置他们的高度。
  • 这会让球回到它开始的位置吗?我不希望它像在 flappybird 中那样使用重力下降,但如果你明白我的意思,就回到它开始的地方?
  • 啊,好吧。然后只需设置高度 +50 或任何你想要的然后有一个计时器,在它达到一定时间后将高度设置回原来的值。我可能会避免 -50,因为有时代码很挑剔,可能会导致轻微的错误。
  • 对不起,如果我很笨,但你能举一个例子来说明如何做到这一点吗?只是作为一个指导方针,因为我刚刚开始学习如何编程哈哈!
  • 我将把它作为答案提交,因为它不适合这里。

标签: java android sprite


【解决方案1】:
@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;
               }  
    }


}

然后您需要将计时器向下添加,或者您可以添加向下按钮。不确定顶部是否也有尖刺。我不知道它应该是什么样子。

【讨论】:

  • 除了“setHeight”显示为错误“无法解析方法”之外,似乎大部分都很好?
  • 用你的方法你没有创建任何东西,所以很难重绘,因为没有要移动的对象。您可以刷新所有内容,摆脱那个球,然后在其上方重新绘制一个相等的球吗?清除 batch.draw(ball, 80, 95, ball.getWidth() * scaleFactor, ball.getHeight() * scaleFactor);等待 5 然后:batch.draw(ball, 80, 145, ball.getWidth() * scaleFactor, ball.getHeight() * scaleFactor);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-09
  • 2014-01-02
  • 1970-01-01
  • 2021-11-17
  • 2021-11-14
相关资源
最近更新 更多