几秒钟后屏幕不会出现

时间:2014-10-06 08:47:14

标签: java libgdx

我希望GameOverScreen在飞机与其中一个岩石相撞几秒后出现,但每当我运行游戏时,GameOverScreen立即出现。 请帮我解决这个问题。 这是代码。

package com.ravicake.motiontest;

import java.util.Iterator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;


public class GameScreen implements Screen, InputProcessor {
final MotionTestGame motionTestGame;

Texture rockImage;
Texture rockImageR;
Texture playerImage;
TextureAtlas planeAtlas;
TextureAtlas sideRockAtlas;
TextureAtlas explosionAtlas;
Animation planeAnimation;
Animation sideRockAnimation;
Animation explosionAnimation;

Texture bg;
Sound dropSound;
Sound bombSound;
// Music rainMusic;
OrthographicCamera camera;
Rectangle player;
Array<Rectangle> rocksArray;
Array<Rectangle> rocksArrayR;
long lastRockTime;
long lastRockTimeR;
long explosionTime;
int speed = 400;
int lives = 3;
public float posX, posY;
float elapsedTime = 0;
boolean repeatAnimation = true;
boolean gameover = false;

public void setDropsCollected(int dropsCollected) {

}

public GameScreen(final MotionTestGame game) {
    motionTestGame = game;
    Gdx.input.setInputProcessor(this);


    rockImage = new Texture(Gdx.files.internal("rockb.png"));
    rockImageR = new Texture(Gdx.files.internal("rocka.png"));
    planeAtlas = new TextureAtlas(Gdx.files.internal("planeAtlas.pack"));
    sideRockAtlas = new TextureAtlas(Gdx.files.internal("siderock.pack"));
    explosionAtlas = new TextureAtlas(Gdx.files.internal("explosionAtlas.pack"));
    planeAnimation = new Animation(1 / 12f, planeAtlas.getRegions());
    sideRockAnimation = new Animation(1 / 9f, sideRockAtlas.getRegions());
    explosionAnimation = new Animation(1 / 9f, explosionAtlas.getRegions());
    // buck = new Texture(Gdx.files.internal("petrol.png"));
    bg = new Texture(Gdx.files.internal("background1.png"));

    // load the sound effect and the rain background "music"
    // dropSound = Gdx.audio.newSound(Gdx.files.internal("pick.wav"));
    // bombSound = Gdx.audio.newSound(Gdx.files.internal("bomb.wav"));
    // rainMusic = Gdx.audio.newMusic(Gdx.files.internal("flute.ogg"));
    // rainMusic.setLooping(true);
    // load preference file

    // create the camera and the SpriteBatch
    camera = new OrthographicCamera();
    camera.setToOrtho(false, 480, 800);

    // create a Rectangle to logically represent the player
    player = new Rectangle();
    player.x = 480 / 2 - 64 / 2; 
    player.y = 650; 
    player.width = 128;
    player.height = 128;

    // create the rocks array and spawn the first rock
    rocksArray = new Array<Rectangle>();
    rocksArrayR = new Array<Rectangle>();
    spawnRocks();
    spawnRocksR();

}

private void spawnRocks() {
    // TODO Auto-generated method stub
    Rectangle rock = new Rectangle();
    rock.x = MathUtils.random(-150, -100);
    rock.y = -130;
    rock.width = 256;
    rock.height = 128;
    rocksArray.add(rock);
    lastRockTime = TimeUtils.nanoTime() + 1000000000
            * MathUtils.random(1, 2);

}

private void spawnRocksR() {
    Rectangle rockR = new Rectangle();
    rockR.x = MathUtils.random(300, 400);
    rockR.y = -130;
    rockR.width = 256;
    rockR.height = 128;
    rocksArrayR.add(rockR);
    lastRockTimeR = TimeUtils.nanoTime() + 1000000000
            * MathUtils.random(1, 2);


    }


private void crashed() {
    planeAnimation = explosionAnimation;
    explosionTime = TimeUtils.nanoTime();
    System.out.println("timer started");

}

@Override
public void render(float delta) {

    Gdx.gl.glClearColor(0, 0.5f, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // tell the camera to update its matrices.
    camera.update();

    // tell the SpriteBatch to render in the
    // coordinate system specified by the camera.
    motionTestGame.batch.setProjectionMatrix(camera.combined);
    elapsedTime += Gdx.graphics.getDeltaTime();

    // begin a new batch and draw the player and everything
    //

    motionTestGame.batch.begin();
    motionTestGame.batch.draw(bg, 0, 0);
    motionTestGame.font.setScale((float) 1.5);

    // game.font.draw(game.batch, "Level: " + level, 710, 470);
    motionTestGame.font.draw(motionTestGame.batch,
            "DROPED: " + (3 - lives), 300, 770);
    motionTestGame.batch.draw(
            planeAnimation.getKeyFrame(elapsedTime, repeatAnimation), player.x,
            player.y);
    motionTestGame.batch.draw(
            sideRockAnimation.getKeyFrame(elapsedTime, repeatAnimation), -165, 0);
    motionTestGame.batch.draw(
            sideRockAnimation.getKeyFrame(elapsedTime, repeatAnimation), 413, 0);

    for (Rectangle rock : rocksArray) {
        motionTestGame.batch.draw(rockImage, rock.x, rock.y);
    }
    for (Rectangle rockR : rocksArrayR) {
        motionTestGame.batch.draw(rockImageR, rockR.x, rockR.y);
    }
    motionTestGame.batch.end();

    // / accelerometer controls
    player.x -= Gdx.input.getAccelerometerX() * 3;

    // process user input
    if (Gdx.input.isTouched()) {
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        camera.unproject(touchPos);
        player.x = touchPos.x - 64 / 2;

    }
    if (Gdx.input.isKeyPressed(Keys.LEFT))
        player.x -= 400 * Gdx.graphics.getDeltaTime();
    if (Gdx.input.isKeyPressed(Keys.RIGHT))
        player.x += 400 * Gdx.graphics.getDeltaTime();

    // make sure the player stays within the screen bounds
    if (player.x < 0)
        player.x = 0;
    if (player.x > 480 - 64)
        player.x = 480 - 64;

    // check if we need to create a new rocks
    if (TimeUtils.nanoTime() - lastRockTime > 1000000000) {
        spawnRocks();
    }

    if (TimeUtils.nanoTime() - lastRockTimeR > 1000000000f) {
        spawnRocksR();

    }

    if (TimeUtils.nanoTime() - explosionTime > 7000000000f) {

        gameover=true;
    }



    // ////////// left side rocks
    Iterator<Rectangle> iter = rocksArray.iterator();
    while (iter.hasNext()) {
        Rectangle rock = iter.next();
        rock.y += speed * Gdx.graphics.getDeltaTime();
        if (rock.y + 128 > 1000) {
            iter.remove();

        }

        if (rock.overlaps(player)) {

            crashed();
            //repeatAnimation = false;
        }
    }

    // //////// right side rocks
    Iterator<Rectangle> iter2 = rocksArrayR.iterator();
    while (iter2.hasNext()) {
        Rectangle rockR = iter2.next();
        rockR.y += speed * Gdx.graphics.getDeltaTime();
        if (rockR.y + 128 > 1000) {
            iter2.remove();

        }
        if (rockR.overlaps(player)) {

            crashed();

        }
    }

    if(gameover){
        motionTestGame.setScreen((new GameOverScreen(motionTestGame)));
        dispose();
    }
}





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

@Override
public void show() {
    // start the playback of the background music
    // when the screen is shown
    // rainMusic.play();

}

@Override
public void hide() {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void dispose() {
    // playerImage.dispose();
    rockImage.dispose();
    rockImageR.dispose();
    // dropSound.dispose();
    // bombSound.dispose();
    sideRockAtlas.dispose();
    planeAtlas.dispose();
    // rainMusic.dispose();
    bg.dispose();

}


@Override
public boolean keyDown(int keycode) {

    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {

    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}

@Override
public boolean scrolled(int amount) {
    return false;
}

}

1 个答案:

答案 0 :(得分:0)

我会将方法crashed()更改为以下内容:

private void crashed() {
    planeAnimation = explosionAnimation;
    explosionTime = TimeUtils.nanoTime();
    gameover = true;

    System.out.println("timer started");
}

然后在您的方法render()中删除行

if (TimeUtils.nanoTime() - explosionTime > 7000000000f) {
    gameover=true;
}

并修改

if (gameover){
    motionTestGame.setScreen((new GameOverScreen(motionTestGame)));
    dispose();
}

进入

if (gameover && TimeUtils.nanoTime() - explosionTime > 7000000000f){
    motionTestGame.setScreen((new GameOverScreen(motionTestGame)));
    dispose();
}

此外,您可能希望将!gameover && rockR.overlaps(player)添加到if语句中,以避免多次调用crashed()并重置计时器。