# How to make a sprite jump up and then return to original position android java

I am just starting out in game development and I am trying to make a simple android game using GDXLib. I want to make my sprite "ball" jump straight up in the air and then return to where it originally was. How would I do this? I have looked at multiple other answers from similar questions but none seem to work with my code. This is my code so far:

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

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

}

@Override
public void dispose () {

}


}

• What do you mean by None of them seems to work with my code? Please show us what you have tried. As it is, this looks more like a code sample request than an actual question. Feb 3 '17 at 16:09
• This is just my basic code that I have written before adding in what I need to make my sprite jump. Feb 3 '17 at 16:11
• Basically it is a code sample request, yes. I was just looking for some help on how to make a sprite jump as I am still not sure how to do this. Feb 3 '17 at 16:13
• Please edit your question to add what you have tried. Feb 3 '17 at 16:13
• Ok. Now that we know what you have tried, can you tell us what exactly is not working? Is the KeyDown method never called? Is it called but instead of jumping the ball gets taller? Feb 3 '17 at 16:18

First of all, your ball should not be a texture. IMHO it should have a texture.

That means you should create an ball object which has a texture as an attribute. This object can then have x and y attributes as well as width and height attributes that you can change at will.

If you look at the libgdx documentation you see that Texture has no such thing as a setHeight method, and that makes sense as your texture has a set width and height.

When you have your ball object, it is not the height you should change, but rather the position y.

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


Like this your ball will elevate itself as long as you keep UP pressed. (If it is going down instead it means the y origin is at the top so you should do -50 instead).

Finally you can draw the ball like this

batch.draw(ball, ball.x, ball.y, ball.getWidth() * scaleFactor, ball.getHeight() * scaleFactor);


Try making this work first, and then you can think about making the ball go down to the origin or about making the jump more "natural".

• Ok thanks, that's very helpful. I was just thinking, I would need to change this if I wanted the sprite to jump when the screen is touched rather than a key is held down wouldn't I? Think I got a bit confused with that. Anyway, thanks for your time. Feb 3 '17 at 16:48
• You will just need to change how you get the input from the user. Just look for "How to get user touch input, libgdx" to get plenty documentation about this. Feb 3 '17 at 16:56