简体   繁体   中英

Why does a code run after setScreen() in LIBGDX?

I have a simple menu screen whith four buttons and event listeners on each of them. In this listeners I execute changeScreen() method, which changes the screen by invoking setScreen() . Screens change whithout problems but why does the code after changeScreen() in the body of the changed() method run.

openFile.addListener(new ChangeListener() {
    @Override
    public void changed(ChangeEvent event, Actor actor) {
        parent.changeScreen(Hren.OPENFILE);
        System.out.println("Why do I see this text when I am in OPENFILE screen");
    }
 });

There is something called the call stack in most programming languages. Every method you call will run until it hits a line that calls return . (There is an implicit return right at the end of the method if you don't type one.) When you call a method, it is added to the stack so it is run next before it is popped off and execution can continue from where it was added.

In this case, once changeScreen() returns, it is popped off the stack and execution continues on through the rest of this changed() method. It doesn't matter that whatever called it is currently no longer queued for drawing on the screen. The call stack doesn't know or care about that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM