I've encountered something strange in my LibGDX project. I'm currently using a Music instance to play my ambient music, which itself is a copy of another external static Music instance. My problem comes when I swap screens and then swap back to the previous screen - the audio playback becomes noticeably higher in volume even though the music.getVolume() returns the same value. I've already made sure that I stop the music from playing and I dispose of the music object, so I don't think it's because more than one music instance is overlapping.
Code:
//In my assets class
public static final Music RAIN_AMBIENT_MUSIC = Gdx.audio.newMusic(Gdx.files.internal("music/ambient/rain.mp3"));
//In my screen's show() method
background_music_1 = Assets.RAIN_AMBIENT_MUSIC;
background_music_1.setLooping(true);
background_music_1.play();
background_music_1.setVolume(0.5f);
//In my screen's hide() method
background_music_1.stop();
dispose();
//In my screen's dispose method
background_music_1.dispose();
Any ideas?
Related
I have an android application consisting of 4 different screens. I am currently creating a new camera for each screen. Each of these screens have the same height and width. Is there a better way to handle this camera, rather than creating a new one for each screen, or is that necessary?
You could make a class like GameObjects and declare there your public static camera.
public static Camera camera;
Now in every screen you got, in the show method you would probably need to reset what you changed to it.
Example, if your camera was moving in a screen, you might want to set the position of the camera again in the show method (same thing about zoom, rotation etc).
You call your camera functions with GameObjects.camera, ex: GameObjects.camera.update();
I create a camera and spritebatch for every screen I have, and it's ok! Don't worry too much about it.
I don't really think that making just one camera is a good ideea anyways.
I am creating an android application composed of three screens and a game object that is passed between them.
A MenuScreen that is only used when the game is started. This makes no change to the GameObj and simply sets the screen to the GameScreen.
The GameScreen where the player dodges falling blocks while the GameObj.timer measures the time the player is alive. When the player collides with a falling object, the screen is set to the EndScreen.
The EndScreen lists the time the player was alive by accessing the GameObj.Timer and has a button to set the screen back to the GameScreen.
The information is passed from the GameScreen to the EndScreen properly the first time, but when the Reset button is pressed and the screen is set to GameScreen, the timer is NOT reset back to 0 and the blocks from the previous game are still present.
Initially I had used GameObj.setScreen(new GameScreen(this)); but this creates a leak because the previous GameScreen is never deleted.
I am wondering if there is a way in LibGDX to dispose the entire screen (rather than just the textures) and create a new one?
Thanks in advance!
I am making a game in LibGDX, and I have noticed that when my game is launched a black screen is shown for a few moments before the white screen of my game is drawn.
I have set the background of my app's theme to be a different color with android:windowBackground in my app's style.xml, and the preview window shows that color. However, right before the main screen of my app loads, the screen goes black for a moment before displaying the main screen.
I've tried setting Gdx.gl.glClearColor(1,1,1,1) in the main activity and Game class of my game, but the black screen still shows for a moment before drawing the main screen.
The only way I've found to fix this is to set the android:windowDisablePreview to true, but that disables the preview window all together.
Is there any way to fix this without disabling the preview?
In addition to shifting objects creation to constructor, you also need to shift the ApplicationListener object creation in Activity class's constructor.
The black screen is probably because of the time delay between start of Activity.onCreate() call and end of ApplicationListener.show() call.Try to measure it using a timer or simply System.currentTimeMillis() so the difference would be visible.
This should improve the situation if not eliminate the problem.
Here is my problem:
I currently have on Canvas in my main game activity that is constantly being drawn with OnDraw(). This is a board game with two players. When a player's turn begins, there are many options that the player can do, and I would like all of this to be put in a different class.
Also, this new class must draw NEW animations on top of the current canvas. These animations will came from attacks (I haven't even looked into animations yet). So basically I would like another canvas on top of the main one.
Furthermore, the OnDraw function in the main activity must PAUSE and wait for the other class with the canvas on top to complete.
Let me try and summarize this: I have a main game class with one canvas. This class handles the players' turns and setting up the game. It also draws the playing field. I need another class, when it is a player's turn, to draw animations and other things on top of the current canvas.
Can anybody help me with this?
NOTE : I looked into Fragments and FragmentManager, but it seems I can only use XML Views with that and not RenderViews.
Thank you!
You might want to consider using multiple bitmaps as buffers and then just add them on top of each other before rendering to the canvas.
There is a nice example in the "samples" pack that comes with the sdk that shows of how you can merge bitmaps on top of each other.
Let me know if you want to try this out and need some more pointers on how to do it :)
How do i detect when my textures are destroyed on android?
My Renderer class for my GLSurfaceView currently looks like this:
public void onDrawFrame(GL10 gl)
{
nativeLibrary.drawFrame();
}
public void onSurfaceChanged(GL10 gl, int width, int height)
{
if (reload)
{
library.glRecreate(); //this method reloads destroyed textures
}
else
{
nativeLibrary.init(width, height)); //this method initializes my game
reload = true;
}
}
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
}
The problem is that doesn't always work. When i press the home button from my game and then start it again, it works like a charm. But when i lock the device, and then unlock it again, all textures are just black. Everything seems to reset when I lock it too (my game always comes back in the main menu). When I quit the game using the home button, and do a lock/unlock after that, the game doesn't reset.
When doing OpenGL on Android, I highly recommend that you watch these two Google I/O talks by Chris Pruett, Android advocate, who wrote the open-source game Replica Island.
Here he talks about the exact problem you're seeing. Long story short: you don't detect when your textures (and buffers) are destroyed, but you detect when they need to be recreated. And this is exactly what the onSurfaceCreated callback is for:
Since this method is called at the beginning of rendering, as well as every time the EGL context is lost, this method is a convenient place to put code to create resources that need to be created when the rendering starts, and that need to be recreated when the EGL context is lost. Textures are an example of a resource that you might want to create here.