I am programming with LIBGDX and I am working with a piece of code. What I want to happen is that when the BUCKET collides with a DROP the game switches screens back to the menuScreen. I have tried to do it as you will see in the code but when the bucket collides with a drop nothing happens. Please Help me on how to fix this. Here is all the code:
DROP CLASS:
public class Drop extends Game{
public SpriteBatch batch;
public BitmapFont font;
public void create() {
batch = new SpriteBatch();
//Use LibGDX's default Arial font.
font = new BitmapFont();
this.setScreen(new MenuScreen(this));
}
public void render() {
super.render(); //important!
}
public void dispose() {
batch.dispose();
font.dispose();
}
}
MENU SCREEN CLASS:
public class MenuScreen implements Screen {
final Drop game;
OrthographicCamera camera;
public MenuScreen(final Drop gam) {
game = gam;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.font.draw(game.batch, "Welcome to Drop!!! ", 100, 150);
game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
game.batch.end();
if (Gdx.input.isTouched()) {
game.setScreen(new GameScreen(game));
dispose();
}
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void show() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}
GAME SCREEN CLASS:
public class GameScreen implements Screen {
Texture dropImage;
Texture bucketImage;
Sound dropSound;
Music rainMusic;
MenuScreen menuScreen;
Drop game;
OrthographicCamera camera;
SpriteBatch batch;
Rectangle bucket;
Array<Rectangle> raindrops;
long lastDropTime;
boolean isBucketJumping;
float bucketYSpeed = 1;
float GRAVITY = 1;
public GameScreen(final Drop gam) {
this.game = gam;
// load the image for the droplet and the bucket, 48X48 pixels each
dropImage = new Texture(Gdx.files.internal("cloud100.png"));
bucketImage = new Texture(Gdx.files.internal("plane100.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();
menuScreen = new MenuScreen(game);
camera = new OrthographicCamera();
camera.setToOrtho(false,800,480);
batch = new SpriteBatch();
bucket = new Rectangle();
bucket.x = 250;
bucket.y= 200;
bucket.width = 100;
bucket.height = 100;
raindrops = new Array<Rectangle>();
spawnRaindrop();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0x64/255.0f, 0x95/255.0f, 0xed/255.0f,
0xff/255.0f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
if(Gdx.input.isTouched())
{
bucketJump();
}
moveBucket();
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 > 2000000000) spawnRaindrop();
moveRainDrop();
}
public void bucketJump(){
isBucketJumping = true;
bucketYSpeed = 7;
}
public void moveBucket(){
bucket.y += bucketYSpeed;
bucketYSpeed -= GRAVITY;
}
@Override
public void dispose() {
dropImage.dispose();
bucketImage.dispose();
dropSound.dispose();
rainMusic.dispose();
batch.dispose();
}
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();
menuScreen.show();
}
}
}
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() {
}
@Override
public void show() {
}
@Override
public void hide() {
}
}