0
\$\begingroup\$

I have been editing a piece of code and I have already implemented gravity so I would like to know how i could make the player jump when the screen is touched. I would like the player bucket to jump. Here is my code you tell me how I add this to it.

public class Drop implements ApplicationListener {

Texture dropImage;
Texture bucketImage;
Sound dropSound;
Music rainMusic;

float GRAVITY  = -5f;

OrthographicCamera camera;
SpriteBatch batch;

Rectangle bucket;
Array<Rectangle> raindrops;
long lastDropTime;

@Override
public void create() {  
    // load the image for the droplet and the bucket, 48X48 pixels each
    dropImage = new Texture(Gdx.files.internal("drop.png"));
    bucketImage = new Texture(Gdx.files.internal("bucket.png"));

    //load the drop sound effect and the rain background "music"
    dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
    rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));

    //start the playback of the background music immediately
    rainMusic.setLooping(true);
    rainMusic.play();

    camera = new OrthographicCamera();
    camera.setToOrtho(false,800,480);

    batch = new SpriteBatch();

    bucket = new Rectangle();
    bucket.x = 250;
    bucket.y= GRAVITY;
    bucket.width = 100;
    bucket.height = 100;        

    raindrops = new Array<Rectangle>();
    spawnRaindrop();
}

/* (non-Javadoc)
 * @see com.badlogic.gdx.ApplicationListener#dispose()
 */
@Override
public void dispose() {
    dropImage.dispose();
    bucketImage.dispose();
    dropSound.dispose();
    rainMusic.dispose();
    batch.dispose();
}

@Override
public void render() {      
    Gdx.gl.glClearColor(0x64/255.0f, 0x95/255.0f, 0xed/255.0f, 
             0xff/255.0f);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    camera.update();

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.draw(bucketImage,bucket.x,bucket.y);
    for(Rectangle raindrop: raindrops){
        batch.draw(dropImage,raindrop.x,raindrop.y);
    }
    batch.end();



    if(TimeUtils.nanoTime() - lastDropTime > 900000000) spawnRaindrop();

    moveRainDrop();
}

private void moveRainDrop() {
    Iterator<Rectangle> iter = raindrops.iterator();
    while(iter.hasNext()){
        Rectangle raindrop = iter.next();
        raindrop.x -= 200 * Gdx.graphics.getDeltaTime();
        if(raindrop.x + 48 < 0) iter.remove();

        if(raindrop.overlaps(bucket)){
            dropSound.play();
            iter.remove();
        }
    }
}

private void spawnRaindrop(){
    Rectangle raindrop = new Rectangle();
    raindrop.y = MathUtils.random(0,450-48);
    raindrop.x = 800;
    raindrop.width = 110;
    raindrop.height = 100;
    raindrops.add(raindrop);
    lastDropTime = TimeUtils.nanoTime();
}


}


@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}
 }
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Setting bucket.y only sets the buckets position, so that should not be -.5, it should be the bottom of the screen (0), or at least the ground, to start. To move the bucket you should add a method called jumpBucket() or something similar. Then call that method whenever a touch is performed (check this within your render() method). This method should add some sort of y-directional speed (stored as a variable in your class) to the bucket and, if you want, set a boolean called isBucketJumping to true.

Then to apply gravity: you generally want similar gravity between all objects on the screen, but since you probably want the raindrops to move without acceleration, you can just leave the moveRainDrop() method alone (although you shouldn't use magic numbers, like the 48 and 200; instead use a constant for the 200 and use raindrop.height for the 48). Make a moveBucket() method and whenever isBucketJumping == true move the bucket upwards by it's y speed and then subtract your gravity value from it's y speed. By playing around with the value for gravity and the balloon's initial y-directional speed you should be able get the bucket to hop as you want it to.

// in your render method
if(Gdx.input.isTouched())
{
   bucketJump();
}
moveBucket(); // this method will be called every tick

// in your bucketJump() method
isBucketJumping = true;
bucketYSpeed = 100;

// in your moveBucket() method
if(isBucketJumping())
{
   bucket.y += bucketYSpeed;
   bucketYSpeed -= GRAVITY;

   if(bucketHitGround()) // I purposely left out the method in this if statement for you to do as practice
   {
      isBucketJumping = false;
   }
}
\$\endgroup\$
2
  • \$\begingroup\$ Could you give me an example of how you would do it to see if I am doing it right. Please \$\endgroup\$ Aug 10, 2014 at 20:42
  • \$\begingroup\$ @user3475907 I gave you a little example :) \$\endgroup\$ Aug 11, 2014 at 2:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .