管理多个场景的后退按钮
我遵循了本教程 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过以下两种方式之一来重写后退键,即重写 onBackPressed() 方法,或
重写
dispatchKeyEvent () 方法。
You can override the back key in one of two ways, either by overriding the onBackPressed() method, or the dispatchKeyEvent() method
Overriding onBackPressed:
Overriding dispatchKeyEvent:
从这个意义上说,我也像你一样陷入了困境。我解释一下。我有一个类似使用 ViewFlipper 的活动。我需要转到我在 ViewFlipper 标签上编写的不同“布局”,并且当我处于不是活动的主要“布局”的特定“布局”时,我需要返回到主要按后退按钮,但是,它不起作用我想要的。
为了解决这个困境,我重写了
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: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.