Overlapping music in a Java game with LibGDX implementation - java

I am having a problem in with this part of the code, I am creating a very simple game in Java with the implementation of LibGDX.
I have three different game states that are handled in three different classes:
StateCreation
MenuState
PlayState
They all work perfectly except for one thing. The music that I have inserted and that is created in the StateCreation should start over, when the player gets to the gameover and the state changes the mode to start over or go back to the menu. This does not happen and the music overlaps
public class GameBarbo extends ApplicationAdapter {
//impostazioni base dell'applicazione
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static final String TITLE = "Cavaliere";
private GameStateManager gsm = new GameStateManager();
private SpriteBatch batch;
private Music music;
#Override
public void create () {
batch = new SpriteBatch();
music = Gdx.audio.newMusic(Gdx.files.internal("sound.mp3"));
music.setVolume(0.1f);
music.play();
Gdx.gl.glClearColor(1, 0, 0, 1);
gsm.push(new MenuState(gsm));
}
#Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render(batch);
}
#Override
public void dispose() {
super.dispose();
music.dispose();
}
}

You code looks very much like what I found in this guide (though they don't include super.dispose().)
Is it possible that when your game state changes, the dispose() method does not execute? I haven't used libgdx in ages, but it could be the case that dispose() only executes when you exit the program.
I see from this wiki that the Music class has a stop() method. Perhaps that needs to be called directly when the game ends. But IDK if stopping the file and executing a reload for another iteration is asking for a memory leak or not.
Hopefully someone with more experience will answer, but maybe these thoughts will help you figure something out in the meantime.

Related

LibGDX Game Pause State: Animation flicker/bug

Im trying to implement a pause option for my following game. By pressing the back button my game should go to the updatePause() method. But instead it renders the latest 2-3 frames repeatedly. Making it look like it's "flickering".
By trying to prevent this I've added (in the comments) non-continously rendering which prevented the flickering. But then Im facing the problem that pressing the back-button more than once will render another frame. (one of those flickering frames). This is my code structure in short:
public class GameScreen implements Screen {
private static final int GAME_RUNNING = 0;
private static final int GAME_PAUSED = 1;
private static final int GAME_OVER = 2;
private static final int GAME_LEVEL_END = 3;
private int gameState;
private GdxGame game;
//create()
public GameScreen(final GdxGame game) {
//create all the stuff
}
#Override
public void render(float deltaTime) {
switch (gameState) {
case GAME_RUNNING:
updateRunning(deltaTime);
break;
case GAME_PAUSED:
updatePause(deltaTime);
break;
case GAME_OVER:
updateGameOver();
break;
}
}
public void updateRunning(float deltaTime) {
//draw section
if (gameState == GAME_RUNNING) {
game.batch.begin();
//Draw some stuff
game.batch.end();
//move section
handleInput();
}
}
public void updatePause(float deltaTime) {
//Gdx.graphics.setContinuousRendering(false);
//resume
if (Gdx.input.isTouched()) {
gameState = GAME_RUNNING;
//Gdx.graphics.setContinuousRendering(true);
return;
}
}
private void handleInput() {
//catch back button
Gdx.input.setCatchBackKey(true);
if (Gdx.input.isKeyJustPressed(Input.Keys.BACK)) {
gameState = GAME_PAUSED;
return;
}
}
}
Any tips to prevent the flickering?
The render() method is supposed to draw something to the screen. If it doesn't, then the contents of the back buffer will be undefined (so may contain a previous frame) and you get this flicker.
Disabling continuous rendering sounds like a good approach. Alternatively, continue drawing even if the game is paused.
As Thomas points out in his answer. It should be drawing every frame.
I haven't see you draw anything on screen yet, so backbuffer might just contain junk information. One way to solve this is to always clear the screen at the top of render(). This means when you switch immediately to another game state, it will make sure that screen is clear and no flickering result as you experienced.
public void render(float deltaTime) {
// clear screen
Gdx.gl.glClearColor(0f, 0f, 0f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
...
}
As Thomas said: continue drawing is a way to fix it. So I drew the latest frame and disabled continous rendering after that. The result is having no bug at all and giving the CPU a rest.

Wrote the whole game in one class MyGdxGame. How do i mae the main menu? libgdx

I have a big problem.I wrote the whole game in same(one) class MyGdxGame extends from the ApplicationAdapter. How do I make the main menu? So that when opening the game, the main menu opens, in which 1 button, when pressed, opens the game itself (MyGdxGame)? Just tell me how to do it (what to create, what to change)
It's better to achieve high cohesion but when you code inside one class, cohesiveness become very low.
Use Screen and Game class to implement more than one screen as in your case like MainScreen, GameScreen, LevelScreen...
But if you don't want to implement that use flags for different screen in your MyGdxGame class.
public enum GameScreen{
MENU_SCREEN, GAME_SCREEN, LEVELSCREEN;
}
In render method of your MyGdxGame
public GameScreen currentScreen=GameScreen.MENU_SCREEN;
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(currentScreen==GameScreen.MENU_SCREEN){
//render objects for Menu Screen
}else if(currentScreen==GameScreen.GAME_SCREEN){
//render objects for Game Screen
}
}
Change value of currentScreen when you want to move on other screen.
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(currentScreen==GameScreen.MENU_SCREEN){
//detect button of MenuScreen by bounds or any other way
if(check in bound){
currentScreen==GameScreen.GAME_SCREEN;
}
}else if(currentScreen==GameScreen.GAME_SCREEN){
// for Game Screen
}
return false;
}
EDIT
Pelocho suggested to use abstract method inside enum.
public enum GameScreen{
MENU_SCREEN{
#Override
public void render(MyGdxGame gdxGame) {
}
}, GAME_SCREEN{
#Override
public void render(MyGdxGame gdxGame) {
}
}, LEVELSCREEN{
#Override
public void render(MyGdxGame gdxGame) {
}
};
public abstract void render(MyGdxGame gdxGame);
}
And inside render method of MyGdxGame
#Override
public void render() {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
currentScreen.render(this);
}
You should do it properly instead of hacking a menu in. If you look at the Screen class it has the about the same methods as the ApplicationAdapter (show, render, resize, dispose, hide, pause, resume). If you would create a new class and have it implement Screen you can start to cut/paste from your main class to this screen class. If you have something in the Create method then either put it in the Show method or in the constructor. You then create another Screen for your main menu, options, credits, etc.
Have you main class MyGdxGame extend from Game instead of ApplicationAdapter delete all methods except the create method. In the create method you can load your assets and call your first screen like this setScreen(new MenuScreen()). For changing screens you have several options.
Pass the Game object allong with the screens so you can do game.setScreen(..)
Have some sort of screen manager with public static methods to change screens.
Gdx.app.getApplicationListener can be casted into game. This way you can change screen from anywhere in your LibGDX app. ((Game)Gdx.app.getApplicationListener).setScreen(myScreen)
If you are feeling scared you will destroy functionality, make a backup of you game in its current state. Abstracting code from big classes to new smaller classes is a important task for each programmer so it will be good practice. If you feel at any point that a certain piece of code should belong to another/new class feel free to do so. There are many ways to do things and keeping your code readable and understandable is your number 1 priority.

How to manage screens

i'm making a game in libgdx but i'm new and i donĀ“t know if my code is the correct form or the best way to do it.
i'm trying to make the basis of the game:
a screen manager to implement a Menu,
an option screen
a loading screen
a game screen
a pause Screem
a game over screen
In the loading screen I like to do something like Dragon Ball when the loading screen allow us to make goku eat rice, the pause screen needs to draw some statistics and options something like Megaman or Castlevania, the menu do something when the player touch a button something like move the camera or open a door, or move to another "scenario" showing another options, and the game over screen needs to previously gives the player the opportunity to win some life to not lose the game.
I try to make this in this form:
I have the main class of the game with the constructor, and the overriden metods create(), render(), resize() and dispose(), the variables for a camera, a viewport, a Spritebatch, a int to store the current screen, and a manager for the screens.
public class BOS_Project extends Game {
OrthographicCamera camera;
Viewport viewport;
SpriteBatch batch;
AssetManager manager = new AssetManager();
int actualScreen = 0;
public BOS_Project(){
camera = new OrthographicCamera();
camera.setToOrtho(false, screenWidth, screenHeight);
viewport = new FitViewport(screenWidth, screenHeight);
}
#Override
public void create(){
batch = new SpriteBatch();
}
#Override
public void render(){
actualScreen = 1;
ScreenManager.getInstance().showScreen(1, this);
super.render();
}
#Override
public void resize(int width, int height){
viewport.update(width, height);
}
#Override
public void dispose(){
batch.dispose();
}
}
Also the class ScreenManager is singleton, and
public class ScreenManager{
private static ScreenManager instance;
private Game game;
Screen screen;
private ScreenManager(){
super();
}
public static ScreenManager getInstance(){
if(instance == null){
instance = new ScreenManager();
}
return instance;
}
public void initialize(Game game){
this.game = game;
}
public void showScreen(int currentscreen, BOS_Project game){
if(currentscreen == 1){
if(screen!=null)
screen.dispose();
screen = new LoadingScreen(game, game.actualScreen);
game.setScreen(screen);
}else if(currentscreen == 2){
if(screen!=null)
screen.dispose();
screen = new GameScreen(game);
game.setScreen(screen);
}else{
if(screen!=null)
screen.dispose();
screen = new MenuScreen(game);
game.setScreen(screen);
}
}
The other classes are the MenuScreen, GameScreen and Loading Screen.
Loading Screen:
public LoadingScreen(BOS_Project game2, int screen2){
game = game2;
screen = screen2;
game.manager.load("button.png", Texture.class);
game.manager.finishLoadingAsset("button.png");
sprite1 = new Sprite(game.manager.get("button.png", Texture.class));
sprite1.setPosition(0, 0);
//This is a method to load the assets for the especific screen
game.load(screen);
}
#Override
public void render(float delta){
if(game.manager.getProgress()==1) {
if (time < 3) {
Gdx.app.log("Loading: ", "90.0");
}
}else {
Gdx.app.log("Loading: ", String.valueOf(game.manager.getProgress() * 100));
}
game.manager.update();
batch = game.batch;
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite1.draw(batch);
if(game.manager.getProgress()==1 && time > 3){
if(screen==1) {
ScreenManager.getInstance().showScreen(2, game);
}else{
ScreenManager.getInstance().showScreen(3, game);
}
}else{
time += delta;
}
batch.end();
super.render(delta);
}
The Menu and Game classes are similar to loading, only call the assets and draw some sprite on the render method.
This code function well to change screens but I don't know if is the correct form, and another big question is how to manage the pause screen, because I manage this only storing a variable if the variable is pause the render method draw something, if not draw the normal game, but if I want to change the options and images of the pause I need to check variables to know what the pause needs to draw.
if(pause){
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
spritePause.draw(batch);
batch.end();
}else if(game){
batch.begin();
spriteGame.draw(batch);
batch.end();
}
So, am i doing it right? or what would you recommend me? Some examples or maybe specific tutorials to achieve this would be great.
There is not only one correct way of managing screens. Libido gives you the freedom to do it exactly how you please. You can extend the Screen class to make screens for the different needs of your project. You can manage your screens as states and manage them with a state manager.
Regarding the Pause and Game Over screen, you can do that in different ways too.
You can have a dedicated screen to switch to or you can have a Pause and GameOver state in your Game state/screen and show the Pause/GameOver elements on top of your gamescreen while the game is still in the background.
So there is not a single correct way of doing it. The correct way is what works for your game.
For some inspiration:
pixnbgames.com : How to manage screens
Brent Aureli : Game State Manager
As #IronMonkey has answered, there is no "correct" way.
I do it with one GameScreen class only, and have a lot of different states inside the GameScreen class. The GameScreen class is very simple and short (100-200 LOC), and is responsible for switching between states. States are similar to the GameScreen class, but they can be entered and exited without the need to instantiate new objecs.
class GameScreen extends ScreenAdapter implements Disposable {
GameState menuState;
GameState playState;
GameState currentState;
public GameScreen(final MyGame game) {
menuState = new MenuState(this);
playState = new PlayState(this);
}
public void setState(GameState state) {
if(currentState == state) return;
// states have a method 'exit' where you can pause them or finalize something
if(currentState != null)
currentState.exit();
currentState = state;
// also states have 'enter' where you can unpause or init something when entering
currentState.enter();
}
public void render(float delta) {
currentState.render(delta);
}
}
First, this is very resource efficient, because the needed states are created only once when the game is started. Second, because you never destroy the states, but just change them and call enter/exit, they always retain perfectly their current state, i.e. variables, objects and everything and when you go back to a state, it is exactly as it was before you left it.
GameState is interface and you are free to implement it as you wish.
public interface GameState
{
public void enter();
public void exit();
public void update(float delta);
public void render(SpriteBatch batch, float delta);
public String toString();
public void dispose();
}

libGDX Android white screen after back button pressed

I am having a problem with libGDX where when I resume the application after exiting with the back button, I get only a white screen.
The actual app runs, accepts touch input, and plays sounds, but the screen is just white.
I have read that keeping static references to Textures may cause this problem but I am not doing that.
Below is a simplified version of how my asset code works.
public class GdxGame extends Game {
private Assets assets;
#Override
public void onCreate() {
assets = new Asstes();
assets.startLoading();
/*
*Within SplashScreen, GdxGame.getAssets() is accessed, and
*manager.update() is called
*/
setScreen(new SplashScreen());
}
#Override
public void dispose() {
assets.dispose();
assets = null;
}
//Perhaps a problem??
public static Assets getAssets() {
return ((GdxGame) Gdx.app.getApplicationListener()).assets;
}
}
public class Assets implements Disposable{
private AssetManager manager;
public Assets() {
manager = new AssetManager();
}
public void startLoading() {
manager.load(....);
}
#Override
public void dispose() {
manager.dispose();
}
}
Upon returning to the application after the back button is pressed, the AssetManager is recreated, the SplashScreen is reopened (as white), and the AssetManager updates until all assets are reloaded (takes about 2 seconds).
So when the application is reopened, a new AssetManager is loading all of the necessary textures, but for some reason everything is still white.
Could it have something to do with how I access the AssetManager from my UI and Game classes?
//In GdxGame
public Assets getAssets() {
return ((GdxGame) Gdx.app.getApplicationListener()).assets;
}
That is the only place where I could see something going wrong, but even still, I don't understand what could be wrong with that.
Any help would be much appreciated.
Edit:
Here is the SplashScreen class. This makes the issue even more confusing. In the SplashScreen class I load, draw, and dispose a new Texture for the logo. Nothing to do with the AssetManager. Returning after the back button is pressed, this new Texture does not appear either.
public class SplashScreen extends Screen {
private Texture logo;
private Assets assets;
#Override
public void show() {
assets = GdxGame.getAssets();
logo = new Texture(Gdx.files.internal("data/logo.png"));
}
#Override
public void render(float delta) {
super.render(float delta);
if(assets.load()) {
//Switch screens
}
//getBatch() is the same form as getAssets() ((GdxGame) Gdx.app.getApplicationListener()).batch)
GdxGame.getBatch().draw(logo, 100, 100, 250, 250);
}
#Override
public void hide() {
super.hide();
logo.dispose();
}
}
It turns out the issue was to do with how I was managing screens. I had a convenience method in my GdxGame class that allowed me to switch screens based on a ScreenType enum.
Within the enum, I would create a new screen with reflection, given the screen's class in the enum constructor.
The problem is that I stored the screens in the enums, and only created them once. This means that I was keeping and using screens from the old context when I returned after pressing back.
To solve this, I simply had to change it so that a new screen is created every time the enum is accessed, instead of storing one at the start.

AndEngine, PhysicsWorld not updating?

I'm not entirely sure what I'm doing wrong. I have written this code multiple times before without problems. The problem is that the physics world just does not seem to be updating and I don't know why! Right now I have a gameScene class which extends a normal scene, this is the onCreate method:
#Override
public void create() {
ParallaxBackground background = new ParallaxBackground(0, 0, 0);
background.attachParallaxEntity(new ParallaxEntity(0, new Sprite(0, 0, res.BasicBackground, res.vbom)));
this.setBackground(background);
test = new Sprite(0, 0, res.GrassRegion, res.vbom);
FixtureDef fix = PhysicsFactory.createFixtureDef(10f, 0f, 10f);
Body bod = PhysicsFactory.createBoxBody(physicsWorld, test, BodyType.DynamicBody, fix);
attachChild(test);
physicsWorld = new PhysicsWorld(new Vector2(SensorManager.GRAVITY_EARTH, SensorManager.GRAVITY_EARTH), false);
physicsWorld.registerPhysicsConnector(new PhysicsConnector(test, bod, true, true));
this.registerUpdateHandler(physicsWorld);
}
(I removed a lot of the unnecessary things) I just have a basic test sprite to make sure that the physics is working (which it is not). The app runs without any errors, its just that the sprite will not move, leading me to think that its a problem with the physics world being update.
As you can see, I'm setting the gravity to be accelerating in both the X and Y directions. I did this because I thought maybe it wasn't working in one direction for some reason. I'm thinking that maybe I have to set the scene to update in the main activity? But I don't remember having to do that with any of my other apps. Any help is appreciated thank you :)
No, this.registerUpdateHandler(physicsWorld); will do just fine. Try adding this:
physicsWorld = new PhysicsWorld(new Vector2(0, AHC.WORLD_GRAVITY), false) {
#Override
public void onUpdate(float pSecondsElapsed) {
super.onUpdate(pSecondsElapsed);
}
};
And put a breakpoint or Log.i(TAG, "hey"); inside onUpdate to check if it hits it
EDIT:
Try registering a "normal" UpdateHandler, you can do it in 2 ways:
public class YourGameActivity implements IUpdateHandler{
...
private void yourSetupMethod(){
registerUpdateHandler(this);
}
#Override
public void reset() {}
#Override
public void onUpdate(float pSecondsElapsed) {
Log.i(TAG, "hey");
}
}
or you can create a class that implements IUpdateHandler and then:
public class YourGameActivity{
private MyClassThatImplementsIUpdateHandler mInstance;
...
private void yourSetupMethod(){
registerUpdateHandler(mInstance);
}
}
It's the same.
Try it and see if it logs something, if for some reason you can't see the log use a breakpoint just to be sure.

Categories

Resources