How to clear libgdx listeners - java

So in menu i have 3 buttons and all are with the same code. For the topic only important thing is
button2.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
game.setGame();
dispose();
return true;
}
});
Then everything is going fine until:
button3.clearListeners(); // MainMenu.java:174 line in exeption
and then exeption pops out:
Exception in thread "LWJGL Application" java.lang.IllegalStateException: Invalid between begin/end.
at com.badlogic.gdx.utils.DelayedRemovalArray.clear(DelayedRemovalArray.java:125)
at com.badlogic.gdx.scenes.scene2d.Actor.clearListeners(Actor.java:261)
at com.racostyle.avdelux.MainMenu.dispose(MainMenu.java:174)
at com.racostyle.avdelux.MainMenu$3.touchDown(MainMenu.java:123)
at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java:55)
at com.badlogic.gdx.scenes.scene2d.Actor.notify(Actor.java:165)
at com.badlogic.gdx.scenes.scene2d.Actor.fire(Actor.java:136)
at com.badlogic.gdx.scenes.scene2d.Stage.touchDown(Stage.java:277)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:300)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:200)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
EDIT: if i don't remove listeners they are still active and can be clickable, afc with exeptions.

It looks like you're calling MainMenu.dispose within an actor.touchdown listener. MainMenu.dispose clears all listeners on a button. LIBGX won't allow that.
Why? Because LIBGDX is in the process of calling touch events on a lot of actors. You really don't want your game functioning different depending on the order buttons were added to your stage.
So, call clear listeners somewhere else.
Since you're doing this within a dispose method anyway, I'd suggest NOT removing your button listeners at all, as the actor you've registered listeners for should not be used again.

Its not allowed to just call clearListeners() without event;
Will you post your codes in jsfiddle.. i will try to debug it and add some codes..
Thanks ..

Related

Can i remove Inputs and Actions from a java FXGL game

We are using FXGL to programm a little game.
At the beginning we initialized a few UserActions with KeyCodes for inputs.
But later this UserActions shall get deleted/removed.
I found the possibility to rebind an action to a new the KeyCode. But i can't figure out, how to remove this action.
This is what i found and use actually to rebind the Action EXIT_GAME from KeyCode.D to KeyCode.L:
public static void initializeGameInputs(){
//Todo delete this workaround for waiting time - otherwise game crashes
getInput().mockKeyRelease(KeyCode.G);
getInput().mockKeyRelease(KeyCode.H);
getInput().rebind(getInput().getActionByName(EXIT_GAME), KeyCode.L);
getInput().rebind(getInput().getActionByName(START_GAME), KeyCode.K);
getInput().addAction(new WalkUserAction(), KeyCode.D, VirtualButton.RIGHT);
getInput().addAction(new WalkLeftUserAction(), KeyCode.A, VirtualButton.LEFT);
getInput().addAction(new JumpUserAction(), KeyCode.W, VirtualButton.UP);
}
But i want to delete this action.
Thank you a lot for your help!

How to trap the [x] in closing a Shell

I have the following Shell listener code
private class ShellListener extends ShellAdapter
{
#Override
public void shellClosed( ShellEvent e )
{
}
#Override
public void shellDeactivated( ShellEvent e )
{
}
}
I need to be able to trap when the Shell loses focus, ie: the user goes to another application. The shellDeactivated() does that. I also need to know when I explicitly close() the Shell. The shellClosed() does that.
However when a user clicks on the [x] icon at the top/right corner of the Shell, shellDeactivated() fires, then shellClosed() fires. I need to be able to ignore the shellDeactivated() when the [x] is clicked.
The ShellEvent does not have any pertinent information, it just holds the Shell object, not which Shell control initiated the event.
Is there any way I can trap for the [x] click?
Grumble grumble
Ok, inside shellClosed() I pop-up a message asking the user if they really want to quit. It seems that this is considered a lost focus event (true enough). That is what was firing the shellDeactivated() event.
So the shellClosed() fires, I show a pop-up message, which fires the shellDeactivated() event. And things happen out of order. A simple flag and back to normal :-)

LibGDX logging not showing up in logcat

I cannot get LibGDX logging to work in Android Studio. First i thought I had the same problem as my previous question but made sure my app updated on debug.
playButton.addListener(new ChangeListener()
{
#Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.app.debug("BUTTON", "playButton Pressed");
optionButton.addAction(Actions.moveBy(-200, 0, 2));
}
});
The test action on the option button is carried out but i cannot get the debug log to show up.
The default Log level is LOG_INFO. For the Gdx.app.debug call to work, you must first call Gdx.app.setLogLevel(Application.LOG_DEBUG); once (probably the first line in your Game's constructor so you can easily change it).
Use Gdx.app.log or even System.out.println (write sout (syso in eclipse) and enter );

Error when disposing of OrthagonalTiledMapRenderer

Im running into a problem while disposing of a screen. I am getting this error message when I try to dispose of my OrthogonalTiledMapRenderer. I looked around online and didnt find see any other examples of this or situations where this happened.
Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: buffer not allocated with newUnsafeByteBuffer or already disposed
at com.badlogic.gdx.utils.BufferUtils.disposeUnsafeByteBuffer(BufferUtils.java:507)
at com.badlogic.gdx.graphics.glutils.VertexArray.dispose(VertexArray.java:67)
at com.badlogic.gdx.graphics.Mesh.dispose(Mesh.java:551)
at com.badlogic.gdx.graphics.g2d.SpriteBatch.dispose(SpriteBatch.java:944)
at com.badlogic.gdx.maps.tiled.renderers.BatchTiledMapRenderer.dispose(BatchTiledMapRenderer.java:152)
at org.shawnhenry.rollypauly.screens.GameScreen.dispose(GameScreen.java:264)
at org.shawnhenry.rollypauly.screens.GameScreen.hide(GameScreen.java:238)
at org.shawnhenry.rollypauly.InputHandler.doTap(InputHandler.java:147)
at org.shawnhenry.rollypauly.InputHandler.touchUp(InputHandler.java:117)
at com.badlogic.gdx.backends.lwjgl.LwjglInput.processEvents(LwjglInput.java:305)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:199)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
My implementation is quite simple.
In my main method I initilize the renderer:
renderer = new OrthogonalTiledMapRenderer(world.getTiledMap());
The render method called the renderer to draw objects like:
renderer.render(backgroundLayers);
and
renderer.getSpriteBatch().draw(resume, world.getResumeButton().x, world.getResumeButton().y, world.getResumeButton().width, world.getResumeButton().height);
Here is the trace through my various classes/functions mentioned in the error message:
FROM "InputHandler"
if(mainMenu.contains(x, y)){//Tapped the mainMenu button.
//Gdx.app.log("InputHandler", "Hit menu button!");
gameScreen.getGame().setScreen(new LevelSelectScreen(gameScreen.getGame()));
gameScreen.hide();
}
FROM "GameScreen"
#Override
public void hide() {
dispose();
}
#Override
public void dispose() {
renderer.dispose();
//world.dispose();
}
Any help you can give me would be great. If I dispose of my world class, and not renderer I dont get an error. My concern is that I believe the renderer is a resource heavy object so when leaving the screen it needs to be disposed of.
I believe solved my problem. I figured I was calling dispose twice, but I looked through my entire code and the only place I called dispose() was the one time during the hide() and the hide() was only being called once during this:
if(mainMenu.contains(x, y)){//Tapped the mainMenu button.
//Gdx.app.log("InputHandler", "Hit menu button!");
gameScreen.getGame().setScreen(new LevelSelectScreen(gameScreen.getGame()));
gameScreen.hide();
}
My problem I believe is that when I set the new screen to the LevelSelectScreen() the hide() function is automatically called as the the LevelSelectScreen() gains focus...dispose() is called for the first time. Then it is called again in the very next line when I explicitly call it gameScreen.hide()'.

How to know what action is triggering an event?

How do I know what action is triggering a particular event?
org.w3c.dom.events.EventListener refreshAnnotationsListener = new org.w3c.dom.events.EventListener() {
#Override
public void handleEvent(org.w3c.dom.events.Event event) {
// how do I know the action which triggered this event?
}
};
I guess there is a way to print stack trace using "throw" which also displays the name of the action that is triggering an event. Am not exactly sure how to do this though. Or, is there another way of doing it ?
Thanks,
Sony
You can create a stacktrace any time you want, by creating an exception:
new Exception().printStackTrace();
You don't have to throw it.

Categories