Reputation: 2832
I am developing a simple game in Android using libGDX. Its just a catching game in which there are some items which will be falling from top of the screen, also there is a basket at the bottom and user will scroll the basket to collect those items.After some seconds the game will get finished, now in Render method I have a condition on which I need to over the game say after 30 seconds. So I have to call an activity in which I need to display GAME OVER image but I am not able to figure out how can I call this activity from Render method.
I am new to libGDX, and I dont know how to do this. Can anyone help me to sort out this. Any help would be appreciable. Thanks.
Upvotes: 1
Views: 789
Reputation: 1995
First for displaying the image you should try using a stage will make your life much easier. In the stage you can add as many widgets (Actors) you want and then by just calling stage.draw() they will be rendered for you. Check below how this could be done.
public class GameOver implements com.badlogic.gdx.Screen{
final Drop game;
Image gameOverImage;
Stage gameOverStage; // add this
TextureRegion textureRegion; // Get region from texture
TextureRegionDrawable textureRegionDrawable; // drawable from textureRegion
// O̶r̶t̶h̶o̶g̶r̶a̶p̶h̶i̶c̶C̶a̶m̶e̶r̶a̶ ̶c̶a̶m̶e̶r̶a̶; // No need of this stage's viewport has camera
public GameOver(final Drop gam) {
Log.d("Called ","Called ");
game = gam;
gameOverStage = new Stage(new StretchViewport(yourDesiredwidth, yourDesiredHeight));
// You can use different Viewport class if you want see Viewport in Documentation
textureRegion = new TextureRegion(new Texture(Gdx.files.internal("game_over.png"));
textureRegionDrawable = new TextureRegionDrawable(textureRegion);
gameOverImage = new Image(textureRegionDrawable );
gameOverImage.setBounds(x, y, width, height);
stage.add(gameOverImage);
//c̶a̶m̶e̶r̶a̶ ̶=̶ ̶n̶e̶w̶ ̶O̶r̶t̶h̶o̶g̶r̶a̶p̶h̶i̶c̶C̶a̶m̶e̶r̶a̶(̶)̶; // No need of this see above
// c̶a̶m̶e̶r̶a̶.̶s̶e̶t̶T̶o̶O̶r̶t̶h̶o̶(̶f̶a̶l̶s̶e̶,̶ ̶8̶0̶0̶,̶ ̶4̶8̶0̶)̶; // Same
}
and in the render method call stage.draw() so all the actors of the stage get rendered including your image:
@Override
public void render(float delta) {
// TODO Auto-generated method stub
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(); // If you want the actors in stage to move or do some other work
stage.draw(); // Draws all the actors
/* Below is for painting your text although it could be done by creating a Label Actor and add it in stage as the image*/
stage.getBatch().begin();
game.font.setColor(0,0,0,1);
game.font.setScale(3);
game.font.draw(game.batch, "Game Ends!!! ", 100, 150);
game.font.draw(game.batch, "Tap To restart", 100, 100);
stage.getBatch().end();
}
Second for showing a Game Over window you can create a Table, add all your game over actors in the table and then when you want to show it you just add the table to the stage. You can read more about Table in the documentation It's a bit tricky at the beginning but you will ge the rewards later.
Upvotes: 1
Reputation: 42176
This doesn't sound like a job for another Activity. Instead, one option is to use diffrent Screens from within your main Activity. More info here: https://code.google.com/p/libgdx-users/wiki/ScreenAndGameClasses
Alternatively, you could simply keep track of your game's state (plaing, paused, game over, etc) and draw different things depending on that state. Something like this:
public void draw(){
if(state == playing){
//draw game
}
else if(state == gameOver){
//draw game over
}
}
Upvotes: 1