管理多个场景的后退按钮

发布于 2024-12-09 07:21:25 字数 981 浏览 0 评论 0原文

我遵循了本教程 http://www.andengine .org/forums/tutorials/multiple-screen-andengine-game-v2-t4755.html 创建一个具有多个场景和只有一个 Activity 的简单应用程序。 我想知道当我使用后退按钮时如何返回到上一个场景并在第一个场景中完成活动。

我在 MultiScreen 类中尝试过:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        this.mEngine.getScene().back();
    }
    return super.onKeyDown(keyCode, event);
}

this.mEngine.getScene().setChildScene(scene); 替换 SceneManager 中的 core.getEngine().setScene(scene); code>


场景的工作方式与我的理解不同,我解决了:

    @Override
    public void onBackPressed()
    {
        Scene scene = this.mEngine.getScene();
        if(scene.hasChildScene()){
            scene.back();
        }
        else{
            this.finish();
        }
    }

I followed this tutorial http://www.andengine.org/forums/tutorials/multiple-screen-andengine-game-v2-t4755.html to create a simple application with multiple scene and only one activity.
I'd like to know how can i can return to the previous scene when i use the back button and finish the activity when i'm in the first scene.

I tried so in the MultiScreen Class:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        this.mEngine.getScene().back();
    }
    return super.onKeyDown(keyCode, event);
}

replacing the core.getEngine().setScene(scene); in the SceneManager with this.mEngine.getScene().setChildScene(scene);


scene work differently from how I understood, I resolve with:

    @Override
    public void onBackPressed()
    {
        Scene scene = this.mEngine.getScene();
        if(scene.hasChildScene()){
            scene.back();
        }
        else{
            this.finish();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野味少女 2024-12-16 07:21:25

您可以通过以下两种方式之一来重写后退键,即重写 onBackPressed() 方法,或

重写

@Override
public void onBackPressed()
{
    // your code here
}

dispatchKeyEvent () 方法。

@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        // your code here
    }
    return (yourbooleanhere);
}

You can override the back key in one of two ways, either by overriding the onBackPressed() method, or the dispatchKeyEvent() method

Overriding onBackPressed:

@Override
public void onBackPressed()
{
    // your code here
}

Overriding dispatchKeyEvent:

@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        // your code here
    }
    return (yourbooleanhere);
}
东走西顾 2024-12-16 07:21:25

从这个意义上说,我也像你一样陷入了困境。我解释一下。我有一个类似使用 ViewFlipper 的活动。我需要转到我在 ViewFlipper 标签上编写的不同“布局”,并且当我处于不是活动的主要“布局”的特定“布局”时,我需要返回到主要按后退按钮,但是,它不起作用我想要的。

为了解决这个困境,我重写了 OnBackPressed 函数,例如:

@Override
public void onBackPressed() {
    if (condition) {
        // go to the main 
    } else {
        super.onBackPressed();
}
}

where condition,正如名字所示,你怎么知道你在主“布局”中?例如,在我的 ViewFlipper 中,主要“布局”按从 0 开始的数字排序,因此我的主要“布局”是 0 以及我如何知道我处于 0“布局”中,viewflipper.getDisplayedChild ()。如果它返回 0,则我是否留在主“布局”中,否则。

如此简单,但我知道,并不那么优雅。这是一个想法,我认为可以帮助你。

I had a dilemma like you in that sense. I explain. I have an activity like works with a ViewFlipper. I need to go to the different "layout" that I wrote on ViewFlipper tag and, when a I was in a particular "layout" that is not the main "layout" of the activity, I need to come back to the main pressing the back button, but, it doesn't work what I want.

To resolve this dilema, I override OnBackPressed function like:

@Override
public void onBackPressed() {
    if (condition) {
        // go to the main 
    } else {
        super.onBackPressed();
}
}

where condition, as name says, how do you know that you are in the main "layout"? For example, in my ViewFlipper, the main "layout" are ordered by numbers, that begin on 0, so my main "layout" is 0 and how I know that I am in the 0 "layout", viewflipper.getDisplayedChild(). If it returns 0, I stay in the main "layout" or not, otherwise.

So simple but, I know, not so elegant. It's an idea, I think that can help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文