2017-01-23 7 views
-1

私は現在FlappyBirdのスタイルでAndroidのメーカーでゲームをコーディングしています。私は、ユーザーに余分な能力を使用する可能性を与える2 Cooldownsを持っている。保存CURRENTTIME(AndroidStudio/libgdx

ゲームのメインビューで、灰色の画像2枚とその下のタイマーが表示されます。タイマーは10から0になり、ゼロに達すると数字が消え、画像が色づけされ、一時停止画面で能力を有効にすることができます。

問題:

ゲームは、タイマーを開始しているにも開始され、私が望んでいたとしてダウンしました。しかし、ユーザーが一時停止メニューに入ると、タイマーは停止しなければならず、タイマーを離れると再びタイマーが実行されます。

私は

long startTime = 0; 

long elapsedTime = (System.currentTimeMillis() - startTime)/1000; 

それはすべての作品を持っているが、私は長い間に現在の時刻を保存することはできませんし、それのために任意の関数を見つけることができません!

私はコードが非常に長いことを知っていますが、重要な部分はif文のGameState.RunningのdrawWorld()メソッドにあります。

public class PlaneGame extends ApplicationAdapter{ 
private static final float PLANE_JUMP_IMPULSE = 350; 
private static final float GRAVITY = -20; 
private static final float PLANE_VELOCITY_X = 200; 
private static final float PLANE_START_Y = 240; 
private static final float PLANE_START_X = 50; 
private static final int STATE_START = 0; 
private static final int STATE_RUNNING = 1; 
private static final int STATE_OVER = 2; 

SpriteBatch batch; 
OrthographicCamera camera; 
OrthographicCamera uiCamera; 
Texture background; 
TextureRegion ground; 
float groundOffsetX = 0; 
TextureRegion ceiling; 
TextureRegion rock; 
TextureRegion rockDown; 
TextureRegion planeSmall; 
TextureRegion planeSmallBlack; 
Animation plane; 
TextureRegion ready; 
TextureRegion gameOver; 
TextureRegion pause; 
BitmapFont font; 

Vector2 planePosition = new Vector2(); 
Vector2 planeVelocity = new Vector2(); 
float planeStateTime = 0; 
Vector2 gravity = new Vector2(); 
Array<Rock> rocks = new Array<Rock>(); 

GameState gameState = GameState.Start;   /*neeeeeeewwwww*/ 
int score = 0; 
long startTime = 0; 
long startTime2; 
long elapsedTime; 
long savedTime; 
boolean wantToSeeTime = true; 
boolean wantToSeeTimePause = true; 
float timeCdRock = 0; 
float timeCdPlane = 0; 
Rectangle rect1 = new Rectangle(); 
Rectangle rect2 = new Rectangle(); 

Music music; 
Sound point; 
Sound explode; 

//this gets called repeatedly to run the game 
@Override 
public void render() { 
    //clear the screen so we can draw the next one 
    Gdx.gl.glClearColor(1, 0, 0, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    long saveTime = elapsedTime; 
    //update the position of the plane and rocks 
    updateWorld(); 
    elapsedTime = saveTime; 
    //now draw the updated screen 
    drawWorld(); 
} 

//initialize objects and load assets when game starts 
@Override 

public void create() { 
    startTime = TimeUtils.nanoTime();              /*Neeeewwwwwwwww !!!*/ 
    Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener())); 
    batch = new SpriteBatch(); 
    camera = new OrthographicCamera(); 
    camera.setToOrtho(false, 800, 480); 
    uiCamera = new OrthographicCamera(); 
    uiCamera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
    uiCamera.update(); 

    font = new BitmapFont(Gdx.files.internal("arial.fnt")); 

    background = new Texture("background.png"); 
    ground = new TextureRegion(new Texture("ground.png")); 
    ceiling = new TextureRegion(ground); 
    ceiling.flip(true, true); 

    rock = new TextureRegion(new Texture("rock.png")); 
    rockDown = new TextureRegion(rock); 
    rockDown.flip(false, true); 

    Texture frame1 = new Texture("plane1.png"); 
    frame1.setFilter(TextureFilter.Linear, TextureFilter.Linear); 
    Texture frame2 = new Texture("plane2.png"); 
    Texture frame3 = new Texture("plane3.png"); 

    planeSmall = new TextureRegion(new Texture("planeSmall.png")); 
    planeSmallBlack = new TextureRegion(new Texture("planeSmallBlack.png")); 

    ready = new TextureRegion(new Texture("ready.png")); 
    gameOver = new TextureRegion(new Texture("gameover.png")); 
    pause = new TextureRegion(new Texture("pause.png")); 

    plane = new Animation(0.05f, new TextureRegion(frame1), new TextureRegion(frame2), new TextureRegion(frame3), new TextureRegion(frame2)); 
    plane.setPlayMode(PlayMode.LOOP); 

    music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3")); 
    music.setLooping(true); 
    music.play(); 

    point = Gdx.audio.newSound(Gdx.files.internal("point.ogg")); 
    explode = Gdx.audio.newSound(Gdx.files.internal("explode.wav")); 

    resetWorld(); 
} 

//reset the state of the game 
private void resetWorld() { 
    score = 0; 
    startTime = 0; 
    groundOffsetX = 0; 
    planePosition.set(PLANE_START_X, PLANE_START_Y); 
    planeVelocity.set(0, 0); 
    gravity.set(0, GRAVITY); 
    camera.position.x = 400; 

    //randomize the position and direction of the rocks 
    rocks.clear(); 
    for(int i = 0; i < 5; i++) { 
     boolean isDown = MathUtils.randomBoolean(); 
     rocks.add(new Rock(700 + i * 200, isDown?480-rock.getRegionHeight(): 0, isDown? rockDown: rock)); 
    } 
} 

//use the time elapsed since the last call to render() to determine how much to update the game 
private void updateWorld() { 

    float deltaTime = Gdx.graphics.getDeltaTime(); 
    planeStateTime += deltaTime; 

    /*if(Gdx.input.justTouched()) { 
     if(gameState == STATE_START) { 
      gameState = STATE_RUNNING; 
     } 
     if(gameState == STATE_RUNNING) { 
      planeVelocity.set(PLANE_VELOCITY_X, PLANE_JUMP_IMPULSE); 
     } 
     if(gameState == STATE_OVER) { 
      gameState = STATE_START; 
      resetWorld(); 
     } 
    }*/ 

    if(gameState != GameState.Start) planeVelocity.add(gravity); 

    planePosition.mulAdd(planeVelocity, deltaTime); 

    camera.position.x = planePosition.x + 350;  
    if(camera.position.x - groundOffsetX > ground.getRegionWidth() + 400) { 
     groundOffsetX += ground.getRegionWidth(); 
    } 

    rect1.set(planePosition.x + 20, planePosition.y, plane.getKeyFrames()[0].getRegionWidth() - 20, plane.getKeyFrames()[0].getRegionHeight()); 
    for(Rock r: rocks) { 
     //if the rock is off the screen, give it a new location in front of the plane 
     if(camera.position.x - r.position.x > 400 + r.image.getRegionWidth()) { 
      boolean isDown = MathUtils.randomBoolean(); 
      r.position.x += 5 * 200; 
      r.position.y = isDown?480-rock.getRegionHeight(): 0; 
      r.image = isDown? rockDown: rock; 
      r.counted = false; 
     } 
     rect2.set(r.position.x + (r.image.getRegionWidth() - 30)/2 + 20, r.position.y, 20, r.image.getRegionHeight() - 10); 

     //check if the plane crashed 
     if(rect1.overlaps(rect2)) { 
      if(gameState != GameState.GameOver) explode.play(); 
      gameState = GameState.GameOver; 
      planeVelocity.x = 0;     
     } 

     //award a point for not crashing into a rock 
     if(r.position.x < planePosition.x && !r.counted) { 
      score++; 
      r.counted = true; 
      point.play(); 
     } 
    } 

    //check if the plane crashed 
    if(planePosition.y < ground.getRegionHeight() - 20 || 
     planePosition.y + plane.getKeyFrames()[0].getRegionHeight() > 480 - ground.getRegionHeight() + 20) { 
     if(gameState != GameState.GameOver) explode.play(); 
     gameState = GameState.GameOver; 
     planeVelocity.x = 0; 
    }  
} 

//draw the background, rocks, and plane to the screen and possibly some ui text 
private void drawWorld() { 
    elapsedTime = (System.currentTimeMillis() - startTime)/1000; 
    camera.update(); 
    batch.setProjectionMatrix(camera.combined); 
    batch.begin(); 
    batch.draw(background, camera.position.x - background.getWidth()/2, 0); 
    for(Rock rock: rocks) { 
     batch.draw(rock.image, rock.position.x, rock.position.y); 
    } 
    batch.draw(ground, groundOffsetX, 0); 
    batch.draw(ground, groundOffsetX + ground.getRegionWidth(), 0); 
    batch.draw(ceiling, groundOffsetX, 480 - ceiling.getRegionHeight()); 
    batch.draw(ceiling, groundOffsetX + ceiling.getRegionWidth(), 480 - ceiling.getRegionHeight()); 
    batch.draw(plane.getKeyFrame(planeStateTime), planePosition.x, planePosition.y); 
    batch.end(); 

    batch.setProjectionMatrix(uiCamera.combined); 
    batch.begin(); 
    if(gameState == GameState.Start) { 
     batch.draw(ready, Gdx.graphics.getWidth()/2 - ready.getRegionWidth()/2, Gdx.graphics.getHeight()/2 - ready.getRegionHeight()/2); 
    } 
    if(gameState == GameState.GameOver) { 
     batch.draw(gameOver, Gdx.graphics.getWidth()/2 - gameOver.getRegionWidth()/2, Gdx.graphics.getHeight()/2 - gameOver.getRegionHeight()/2); 
    } 
    if(gameState == GameState.GameOver || gameState == GameState.Running) { 
     font.draw(batch, "" + score, Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight() - 60); 
    } 
    if(gameState == GameState.Running || gameState == GameState.Pause) { 
     //font.draw(batch, "" + (10 - elapsedTime), (Gdx.graphics.getWidth()/2)/3, Gdx.graphics.getHeight() - 260); 
     if(wantToSeeTime){ 
      font.draw(batch, "" + (5 - elapsedTime), (Gdx.graphics.getWidth()/2)/3, Gdx.graphics.getHeight() - 260); 
     } 
     if((5 -(elapsedTime) > 0)) { 
      batch.draw(planeSmallBlack, (Gdx.graphics.getWidth()/2)/3, Gdx.graphics.getHeight() - 250); 
     }else { 
      batch.draw(planeSmall, (Gdx.graphics.getWidth()/2)/3, Gdx.graphics.getHeight() - 250); 
      wantToSeeTime = false; 
     } 
    } 
    if(gameState == GameState.Pause){ 
     batch.draw(pause, Gdx.graphics.getWidth()/2 - pause.getRegionWidth()/2, Gdx.graphics.getHeight()/2 - pause.getRegionHeight()/2); 
     if(wantToSeeTimePause){ 
      font.draw(batch, "" + (savedTime), (Gdx.graphics.getWidth()/2)/3, Gdx.graphics.getHeight() - 260); 
     } 
    } 
    batch.end(); 
} 


//object to hold all pertinent information for a rock 
static class Rock { 
    Vector2 position = new Vector2(); 
    TextureRegion image; 
    boolean counted; 

    public Rock(float x, float y, TextureRegion image) { 
     this.position.x = x; 
     this.position.y = y; 
     this.image = image; 
    } 
} 

          // Neeeeeeeewwwww 
static enum GameState { 
    Start, Running, GameOver, Pause 
} 

private class MyGestureListener implements GestureDetector.GestureListener { 

    @Override 
    public boolean touchDown(float x, float y, int pointer, int button) { 
     return false; 
    } 

    @Override 
    public boolean tap(float x, float y, int count, int button) { 
     if(gameState == GameState.Start) { 
      gameState = GameState.Running; 
      startTime = System.currentTimeMillis(); 
     } 
     if(gameState == GameState.Running) { 
      planeVelocity.set(PLANE_VELOCITY_X, PLANE_JUMP_IMPULSE); 
     } 
     if(gameState == GameState.GameOver) { 
      gameState = GameState.Start; 
      resetWorld(); 
     } 
     /*if((gameState == GameState.Pause) && (count == 2)) { 
      planeVelocity.set(PLANE_VELOCITY_X, PLANE_JUMP_IMPULSE); 
      gravity.set(0,GRAVITY); 
      gameState = GameState.Running; 

     }*/ 
     return true; 
    } 

    @Override 
    public boolean longPress(float x, float y) { 

     return false; 
    } 

    @Override 
    public boolean fling(float velocityX, float velocityY, int button) { 
     if(gameState == GameState.Running) { 
      planePosition.set(planePosition.x, planePosition.y); 
      planeVelocity.set(0,0); 
      gravity.set(0, 0); 
      savedTime = System.currentTimeMillis()/1000; 
      wantToSeeTime = false; 
      wantToSeeTimePause = true; 
      gameState = GameState.Pause; 



     } else { 
      planeVelocity.set(PLANE_VELOCITY_X, PLANE_JUMP_IMPULSE); 
      gravity.set(0,GRAVITY); 
      wantToSeeTime = true; 
      wantToSeeTimePause = false; 
      elapsedTime -= savedTime; 
      gameState = GameState.Running; 


     } 

     /*if(gameState == GameState.Pause) { 
      planeVelocity.set(PLANE_VELOCITY_X, PLANE_JUMP_IMPULSE); 
      gravity.set(0,GRAVITY); 
      gameState = GameState.Running; 
     }*/ 

     return true; 
    } 

    @Override 
    public boolean pan(float x, float y, float deltaX, float deltaY) { 
     return false; 
    } 

    @Override 
    public boolean panStop(float x, float y, int pointer, int button) { 
     return false; 
    } 

    @Override 
    public boolean zoom(float initialDistance, float distance) { 
     return false; 
    } 

    @Override 
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) { 
     return false; 
    } 


    public void pinchStop() { 

    } 
} 

}

あなたの助けをありがとう!

+0

ゲームの場合、「System.currentTimeMillis()」を使用しないことが理想的です。このループの各繰り返しの間隔を計算するゲームループメソッドが必要です。この値を 'elapsedTime'に追加します。これはあなたの問題や、異なるハードウェアによって引き起こされるゲームスピードの違いを処理します。 –

+0

しかしあなたの質問に答えるには 'long time = System.currentTimeMillis();がそれを行うべきです。 –

+0

こんにちは、時間を節約するために使用することができます – Shaahul

答えて

0

参照用のリンクを確認してください高度な操作 ためジョダ時間を使用することができます。理想的には、ループが最後に反復されてからどれくらいの時間が経過したかに基づいて、クールダウン中にカウントされる、能力が起動される開始時間が理想的です。たとえば、10秒の再試行の能力の場合:

final long abilityLength = 10 * 1000; 
long cooldownRemaining = 0; 
long lastTimestamp; 
boolean isPaused = false; 

private void doAbility() { 
    if (cooldownRemaining <= 0) { 
     cooldownRemaining = abilityLength; 
    } 
} 

public void main(int[] args) { 
    lastTimestamp = System.currentTimeMillis(); 

    while(true) { // your main game loop 

     // Time (in ms) elapsed since the last iteration of this loop 
     long delta = System.currentTimeMillis() - lastTimestamp; 

     ... // Other game code 

     if (cooldownRemaining > 0 && !isPaused) { 
      // Subtract the delta from the remaing cooldown time. 
      cooldownRemaining -= delta; 
     } 

     lastTimeStamp = System.currentTimeMillis(); 
    } 
}