Java 2D game does not work from menu ? - java

Well I've been developing a 2D java game for a while but I face with a strange problem and I'm stuck :/. So that I'm uploading my source code here.
It is an eclipse project, in this project I have included 2 main methods to clearly state the problem i have experienced:
In Menu class, starts the system from the menu and menu passes the frame to the game manager after pressing play game button
In GameManager class, starts the system by passing a frame to game manager directly.
My problem is that: when I start the system from Menu class and press play game button, rendering continues but system does not respond to any actions even if I had added listeners to canvas, but it works when I start the system from the main method which belongs to GameManager class. :/
Here is my source code: http://www.mediafire.com/?4d2v1jfizrgpaqp
Thanks

Related

How do I initialize a new fxml file (with controller) with a button press?

My program is a smart device controller and what it does is that the user will be able to add their own smart devices via this program. (This is a concept program, so as long as the changes reflect in the program, it is good enough)
How would I even go about initialising a new FXML file and its controller by the press of a button, and relflecting the changes in the main controller and fxml?
I am aware that in the context of my project, I would need to create fxml and controller presets. But after that, I do not know how to start making changes to the program through the actions of the elements inside the program.
This is a basic design of my smart device selection scene
This is how my main menu would look like (I manually added these three devices)
Lets say I choose to add an air-conditioner from the first image, this is how the changes should reflect in the main scene.
Thank you!

Disposing assets and Screens in libgdx

I programmed a game made of multiple screens and every time I need a new screen I use the code
dispose();
game.setScreen(new GameScreen(game));
Inside the dispose method I called the dispose method of each asset like Textures and so on.
I also use the asset manager to load the assets required for the game play screen. In that case when I close the game play screen I also call AssetManager.clear().
Now when I start the game I only have the menu screen and the used memory is about 20MB, then in the game play screen I reach 212MB and when I come back to the menu screen, after disposing the AssetManger, I still have 186MB.
The main problem is that if I start a new game play screen the memory reaches 320MB so after some game play screens the game reaches 700MB!
What's wrong with my code?
You should never have to manually call your Game class's dispose() method. It's called automatically upon changing screens or closing the app. Instead, you should override the Game dispose() like this (code taken from a project I'm working on):
#Override
public void dispose()
{
super.dispose();
assets.dispose();
}
The assets object is my AssetManager. Upon closing the app, I want all my loaded textures to be destroyed from memory. I can't promise anything, but I'm pretty sure that's your problem.

Prevent LibGDX game from completely stopping when paused

In my LibGDX game for android, if the user backs out of the game (either by pressing the home button or switching to another application) LibGDX's built in pause() method is supposed to run. Now, this is fine, and it works fine as well. My problem is that if I back out of the game to do whatever, and then rejoin the game, it has restarted the app completely (kind of like if every time you exited and rejoined in the middle of a game of Pacman your score would be zero and all the dots would be back). For my screen switching, it is necessary that the game NOT restart every time the user exits, but simply enter the corresponding state to actually simulate the 'paused' game. How do I stop LibGDX/Android from killing the game altogether upon user exit, but simply pausing it?
The libGDX application lifecycle matches the lifecycle of the Android Activity as documented in the ApplicationListener interface so you should expect the same behavior. When you press the home button while in a libGDX game then the pause method will be called, which is the same as onPause in Android. The game will go to the background but will stay in memory. However this is not guaranteed and the OS might release the games memory for other applications, there really is no way to get around this. In the case when the game comes back to the foreground and the game restarts you'll need to load the games state from when it was paused.
I've written my own article on how to save and load the game state using Json in libGDX, maybe that will be useful to you.
You should use Asset Manager to prevent it, here a good tutorial :
http://code.google.com/p/libgdx/wiki/AssetManager
the official doc :
http://code.google.com/p/libgdx/wiki/AssetManager
Load all of your textures with assets manager
I have the same problem, but I found the solution by:
Disable option in Developer options called
- Don't keep activities (Destroy every activity as soon as the user leaves it)

Libgdx: Pause menu and Pause state

I have a question regarding the pausing/freezing a game while a window/pausemenu will appear at center of the screen. I've already created a pausescreen class which will be called each time the back key is being pressed.
My problem is that I don't know how to freeze or put the game to pause, this becomes a problem because whenever i tried to click on the outsides of the pause window the player could move around.
superjumper code
follow this link . You will find a gameScreen , there they have specified game state which is responsible for pausing and resuming the game. Read it and your problem will be solved.
All you need is to check current game state in render() function. Here is sample solution: libgdx game how to display pause screen when user click on pause icon

Loading Spalsh Screen before application launching

How can I do to create a loading jframe without title bar like Eclipse in the image...
I used setUndecorate() to remove the title bar and a thread to wait 3 seconds then open the main window but it wouldn't work ...
I'm not thinking to use a progressbar...
How can I do??
Thanks in advance.
Best rgards,
Ali
See the SplashScreen class.
It includes the createGraphics() method that returns a Graphics2D object that can be written to. You would need to draw the progress bar to the graphics object.
Java Web Start also offers splash screen functionality, configured in the launch file. But the graphics object of the JWS splash screen are not available to the app. (so no 'progress bar').
Another way could be what you already thought of: display an undecorated JFrame when the application starts, then instead of just waiting for a few seconds (you could do that as well, if you want users to read some info) just keep initializing the application. When initialization is finished, close the "splash screen" (undecorated JFrame) and open the actual application JFrame.
This would enable you to display dynamic information (like a progress bar if you later want to, some text etc.). If all you need is a static splash screen, go for Andrew's advice and use the built in splash screen functionality.

Categories