2017-03-13 4 views
1

In meinem Gameplay-Bildschirm Ich möchte ein Pause Screen Menü erstellen, die ich die Schaltfläche Wiederholen oder Zurück zum Hauptbildschirm auswählen kann, wenn ich auf die Pause-Schaltfläche klicken. Ich zeichne bereits die Pause-Schaltfläche in meiner Klasse.Mein Problem ist, wie kann ich zeichnen der Pause-Menübildschirm?Pause Bildschirmmenü im Spielbildschirm in LibGdx erstellen?

Hier ist mein Code

//pause 
    pause = new Texture("pause.png"); 
    myTextureRegion = new TextureRegion(pause); 
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion); 
    pause_btnDialog = new ImageButton(myTexRegionDrawable); //Set the button up 
    pause_btnDialog.setPosition(580,1150); 

    stage.addActor(pause_btnDialog); //Add the button to the stage to perform rendering and take input. 
    Gdx.input.setInputProcessor(stage); 
    pause_btnDialog.addListener(new ChangeListener() { 
     public void changed (ChangeEvent event, Actor actor) { 
      System.out.println("Pause Button Pressed"); 
      //Show Pause Screen menu 
      //game.setScreen(new PauseGameday1(game)); 

     } 
    }); 
    stage.addActor(pause_btnDialog); 

GameScreen

public class IngamedayOne implements Screen ,InputProcessor { 

final MyGdxGame game; 
// Constant rows and columns of the sprite sheet 
private static final int FRAME_COLS = 5, FRAME_ROWS = 1; 
private boolean peripheralAvailable; 
private static final float ACCELERATION  = 20f; 

// Objects used 
Animation<TextureRegion> walkAnimation; // Must declare frame type (TextureRegion) 
Texture cat ,left_paw,right_paw,progressbar_background,progressbar_knob,pause,meter; 
Texture carpet,desk,plants,square_carpet,shoes; 
SpriteBatch spriteBatch; 
Sprite sprite; 
private Texture Background; 
ImageButton left_paw_btn,right_paw_btn,pause_btnDialog; 
Viewport viewport; 
private Stage stage; 
// A variable for tracking elapsed time for the animation 
float stateTime; 
private TextureRegion myTextureRegion; 
private TextureRegionDrawable myTexRegionDrawable; 

private boolean isPause; 
private Group pauseGroup; 

//Screen Size 
OrthographicCamera camera; 
float catSpeed = 50.0f; // 10 pixels per second. 
float catX; 
float catY; 
public boolean paused = false; 

public IngamedayOne(final MyGdxGame game) { 
    this.game = game; 
    Gdx.input.setCatchBackKey(true); 
    Gdx.graphics.setContinuousRendering(false); 
    Gdx.graphics.requestRendering(); 
    stage = new Stage(new StretchViewport(720, 1280)); 
    camera = new OrthographicCamera(); 
    camera.setToOrtho(false, 720, 1280); 
    camera.translate(1280/2, 720/2); 
    Gdx.input.setInputProcessor(stage); 
    spriteBatch = new SpriteBatch(); 

    viewport = new StretchViewport(720, 1280); 
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 

    // Load the sprite sheet as a texture 
    cat = new Texture(Gdx.files.internal("cat.png")); 
    sprite = new Sprite(cat); 
    catX=300; 
    catY=0; 

    Gdx.input.setInputProcessor(this); 
    peripheralAvailable = Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer); 
    int orientation = Gdx.input.getRotation(); 
    Input.Orientation nativeOrientation = Gdx.input.getNativeOrientation(); 

    viewport = new StretchViewport(720, 1280); 
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 

    // Progressbar 
    progressbar_background = new Texture("progression_map.png"); 
    progressbar_knob = new Texture("cat_head.png"); 

    //pause 
    pause = new Texture("pause.png"); 
    myTextureRegion = new TextureRegion(pause); 
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion); 
    pause_btnDialog = new ImageButton(myTexRegionDrawable); //Set the button up 
    pause_btnDialog.setPosition(580,1150); 

    stage.addActor(pause_btnDialog); //Add the button to the stage to perform rendering and take input. 
    Gdx.input.setInputProcessor(stage); 
    pause_btnDialog.addListener(new ChangeListener() { 
     public void changed (ChangeEvent event, Actor actor) { 
      System.out.println("Pause Button Pressed"); 
      //Show Pause Screen menu 
      game.setScreen(new PauseGameday1(game)); 

      pause(); 
     } 
    }); 
    stage.addActor(pause_btnDialog); 

    meter = new Texture("meter.png"); 
    //background 
    Background = new Texture(Gdx.files.internal("floor.png")); //File from assets folder 
    // Use the split utility method to create a 2D array of TextureRegions. This is 
    // possible because this sprite sheet contains frames of equal size and they are 
    // all aligned. 
    TextureRegion[][] tmp = TextureRegion.split(cat, cat.getWidth()/FRAME_COLS, cat.getHeight()/ FRAME_ROWS); 
    // Place the regions into a 1D array in the correct order, starting from the top 
    // left, going across first. The Animation constructor requires a 1D array. 
    TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS]; 
    int index = 0; 
    for (int i = 0; i < FRAME_ROWS; i++) { 
     for (int j = 0; j < FRAME_COLS; j++) { 
      walkFrames[index++] = tmp[i][j]; 
     } 
    } 
    // Initialize the Animation with the frame interval and array of frames 
    walkAnimation = new Animation<TextureRegion>(0.200f, walkFrames); 
    // Instantiate a SpriteBatch for drawing and reset the elapsed animation 
    // time to 0 
    spriteBatch = new SpriteBatch(); 
    stateTime = 0f; 

    //left_control 
    left_paw = new Texture(Gdx.files.internal("left_paw.png")); 
    myTextureRegion = new TextureRegion(left_paw); 
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion); 
    left_paw_btn = new ImageButton(myTexRegionDrawable); //Set the button up 
    left_paw_btn.setPosition(10,25); 

    stage.addActor(left_paw_btn); //Add the button to the stage to perform rendering and take input. 
    Gdx.input.setInputProcessor(stage); 
    left_paw_btn.addListener(new InputListener(){ 
     @Override 
     public void touchUp (InputEvent event, float x, float y, int pointer, int button) { 
      System.out.println("Left Button Pressed"); 
      //Start Animation 


     } 
     @Override 
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
      return true; 
     } 
    }); 
    stage.addActor(left_paw_btn); 

    //right_control 
    right_paw = new Texture(Gdx.files.internal("right_paw.png")); 
    myTextureRegion = new TextureRegion(right_paw); 
    myTexRegionDrawable = new TextureRegionDrawable(myTextureRegion); 
    right_paw_btn = new ImageButton(myTexRegionDrawable); //Set the button up 
    right_paw_btn.setPosition(517,25); 

    stage.addActor(right_paw_btn); //Add the button to the stage to perform rendering and take input. 
    Gdx.input.setInputProcessor(stage); 
    right_paw_btn.addListener(new InputListener(){ 
     @Override 
     public void touchUp (InputEvent event, float x, float y, int pointer, int button) { 
      System.out.println("Right Button Pressed"); 
      //Start Animation 
      stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time 
      camera.update(); 
     } 

     @Override 
     public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) { 
      return true; 
     } 
    }); 
    stage.addActor(right_paw_btn); 

} 

public enum State 
{ 
    PAUSE, 
    RUN, 
    RESUME, 
    STOPPED 
} 

private State state = State.RUN; 

@Override 
public void show() { 

} 

@Override 
public void render(float delta) { 
    // clear previous frame 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen 
    stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time 
    camera.update(); 
    spriteBatch.begin(); 
    TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true); 
    spriteBatch.setProjectionMatrix(camera.combined); 
    spriteBatch.draw(Background,0,0); 

    spriteBatch.draw(currentFrame,catX,catY); // Draw current frame at (50, 50) 

    spriteBatch.draw(meter,190,990); 
    spriteBatch.draw(progressbar_background,20,1170); 
    spriteBatch.draw(progressbar_knob,18,1170); 

    //Moving player on desktop 
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) 
     catX -= Gdx.graphics.getDeltaTime() * catSpeed; 
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) 
     catX += Gdx.graphics.getDeltaTime() * catSpeed; 

    if(Gdx.input.isKeyPressed(Input.Keys.UP)) 
     catY += Gdx.graphics.getDeltaTime() * catSpeed; 
    if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) 
     catY -= Gdx.graphics.getDeltaTime() * catSpeed; 

    //Mobile acceleration 
    if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) 
    { 
     catX -= Gdx.input.getAccelerometerX(); 
     catY += Gdx.input.getAccelerometerY(); 
    } 
    if(catY<0) { 
     catY =0; 
    } 
    if(catY> Gdx.graphics.getWidth()-100) { 
     catY =Gdx.graphics.getWidth()-100; 
    } 
    if(catX<0){ 
     catX =0; 
    } 
    if(catX> Gdx.graphics.getHeight()-250) { 
     catX =Gdx.graphics.getHeight()-250; 
    } 

    switch (state) 
    { 
     case RUN: 
      //do suff here 
      break; 
     case PAUSE: 
      break; 
     case RESUME: 
      break; 

     default: 
      break; 
    } 

    spriteBatch.end(); 
    stage.act(); //acting a stage to calculate positions of actors etc 
    stage.draw(); //drawing it to render all 
} 

@Override 
public void resize(int width, int height) { 
    viewport.update(width, height); 
    camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0); 

} 
@Override 
public void pause() { 
    this.state = State.PAUSE; 
} 

@Override 
public void resume() { 
    this.state = State.RESUME; 
} 
@Override 
public boolean keyDown(int keycode) { 

    return true; 
} 
@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; 
} 
@Override 
public void hide() { 

} 

@Override 
public void dispose() { // SpriteBatches and Textures must always be disposed 
    spriteBatch.dispose(); 
    cat.dispose(); 
    left_paw.dispose(); 
    right_paw.dispose(); 
    stage.dispose(); 
    Background.dispose(); 
    progressbar_background.dispose(); 
    progressbar_knob.dispose(); 
} 
} 

Pause-Menü

public class PauseGameday1 implements Screen { 
final MyGdxGame game; 
private Texture Background,pauseImg; 
private Stage stage; 
SpriteBatch spriteBatch; 
OrthographicCamera camera; 
private static final int WIDTH= 720; 
private static final int HEIGHT= 1280; 
private TextureRegion myTextureRegion; 
private TextureRegionDrawable myTexRegionDrawable; 
Viewport viewport; 

public PauseGameday1(MyGdxGame game) { 
    this.game = game; 
    stage = new Stage(new StretchViewport(720, 1280)); 
    camera = new OrthographicCamera(); 
    camera.setToOrtho(false, 720, 1280); 
    camera.translate(1280/2, 720/2); 
    Gdx.input.setInputProcessor(stage); 
    spriteBatch = new SpriteBatch(); 

    viewport = new StretchViewport(720, 1280); 
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
    Background = new Texture(Gdx.files.internal("backgroundimage.png")); //background image 
    pauseImg = new Texture(Gdx.files.internal("pausemenu/pause_text.png")); 

} 

@Override 
public void show() { 

} 

@Override 
public void render(float delta) { 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen 
    camera.update(); 
    spriteBatch.begin(); 
    spriteBatch.setProjectionMatrix(camera.combined); 
    spriteBatch.draw(Background,0,0); 
    spriteBatch.draw(pauseImg,230,900); 

    stage.act(Gdx.graphics.getDeltaTime()); //Perform ui logic 
    spriteBatch.end(); 
    stage.getViewport().apply(); 
    stage.draw(); //Draw the ui 
} 

@Override 
public void resize(int width, int height) { 
    viewport.update(width, height); 
    camera.position.set(camera.viewportWidth/2, camera.viewportHeight/2, 0); 
} 

@Override 
public void pause() { 

} 

@Override 
public void resume() { 

} 

@Override 
public void hide() { 

} 

@Override 
public void dispose() { 
} 
} 

Kann jemand meine Codes korrekt?

+0

ich empfehlen die Verwendung von zwei Bühne, eine für das HUD und eine für das Gameplay, die Lösung für Ihr Problem – Sparcsky

+1

Mögliche Duplikat [Zeichnung Pause Bildschirm als Schicht über dem Spielbildschirm-Libgdx] (http anzuwenden://stackoverflow.com/questions/42578795/drawing-pause-screen-as-a-layer-over-the-play-screen-libgdx) – Aryan

+0

@AbhishekAryan Ich versuche es bereits, aber es funktioniert nicht.Ich habe Fehler in pauseGroup = neue Gruppe; Image semiTransparentBG = ...... // setSize (Größe des Bildschirms) und halb transparent machen. pauseGroup.addActor (semTransparentBG); –

Antwort

0

Sie haben nur pauseGroup deklariert, aber nie in Ihrem Spiel verwendet, rufen Sie pause() Methode von pause_button. Es wird pauseGroup für Sie erstellen und zu Ihrer Bühne hinzufügen. Erstellen Sie in der pause() Methode Actor (UI) und fügen Sie pauseGroup hinzu. Sie können nicht mehrere Bildschirme gleichzeitig mit Ihrer Game Klasse verwenden, da Game die Referenz eines einzelnen Bildschirms hat.

pause_btnDialog.addListener(new ChangeListener() { 
     public void changed (ChangeEvent event, Actor actor) { 
      pause(); 
     } 
}); 


@Override 
public void render(float delta) { 
    // clear previous frame 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear screen 

    camera.update(); 
    spriteBatch.begin(); 
    spriteBatch.setProjectionMatrix(camera.combined); 
    if(this.state==State.RESUME) 
    stateTime += Gdx.graphics.getDeltaTime(); // Accumulate elapsed animation time 
    TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true); 

    spriteBatch.draw(Background,0,0); 
    spriteBatch.draw(currentFrame,catX,catY); // Draw current frame at (50, 50) 

    spriteBatch.draw(meter,190,990); 
    spriteBatch.draw(progressbar_background,20,1170); 
    spriteBatch.draw(progressbar_knob,18,1170); 
    if(this.state==State.RESUME){ 
    //Moving player on desktop 
    if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) 
     catX -= Gdx.graphics.getDeltaTime() * catSpeed; 
    if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) 
     catX += Gdx.graphics.getDeltaTime() * catSpeed; 

    if(Gdx.input.isKeyPressed(Input.Keys.UP)) 
     catY += Gdx.graphics.getDeltaTime() * catSpeed; 
    if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) 
     catY -= Gdx.graphics.getDeltaTime() * catSpeed; 

    //Mobile acceleration 
    if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) 
    { 
     catX -= Gdx.input.getAccelerometerX(); 
     catY += Gdx.input.getAccelerometerY(); 
    } 
    if(catY<0) { 
     catY =0; 
    } 
    if(catY> Gdx.graphics.getWidth()-100) { 
     catY =Gdx.graphics.getWidth()-100; 
    } 
    if(catX<0){ 
     catX =0; 
    } 
    if(catX> Gdx.graphics.getHeight()-250) { 
     catX =Gdx.graphics.getHeight()-250; 
    } 
    } 
    switch (state) 
    { 
     case RUN: 
      //do suff here 
      break; 
     case PAUSE: 
      break; 
     case RESUME: 
      break; 

     default: 
      break; 
    } 

    spriteBatch.end(); 
    stage.act(); //acting a stage to calculate positions of actors etc 
    stage.draw(); //drawing it to render all 
} 

public void pause(){ 

    this.state = State.PAUSE; 
    pauseGroup = new Group; 
    Image semiTransparentBG= ...... 
    // setSize(Size of screen) and make it semi transparent. 
    pauseGroup.addActor(semiTransparentBG); 

    //crate all other pause UI buttons with listener and add to pauseGroup 

    stage.addActor(pauseGroup); 

} 

public void resume() { 

    if(this.state = State.PAUSE){ 
     this.state = State.RESUME; 
     pauseGroup.remove(); 
    } 
} 
+0

Danke, mein Herr. Soll ich Image auf 'Image semiTransparentBG = ......' laden? Ich bekomme Fehler in diesem Teil. –

+0

Image semiTransparentBG = neues Bild (neue Textur (Gdx.files.internal ("backgroundimage.png"))); – Aryan

+1

semiTransparentBG ist nur einer der Actor, erstellen Sie die Anzahl der Actor als Ihre Anforderung und fügen Sie bgGroup hinzu, vergessen Sie nicht, SetSize und Position von Actor als Ihre Anforderung festzulegen – Aryan