屏幕无法在android(libgdx)退出后第二次启动它

时间:2021-12-08 01:28:44

This is the menuscreen class which when called will draw the menuscreen using the spritebatch passed as a parameter. the problem is that the draw command fails to execute the second time around (i.e after we restart the application) while all other features work perfectly (you can still use the buttons we placed on the screen if we click on the position we placed it in )

这是menuscreen类,调用时将使用作为参数传递的spritebatch绘制菜单屏幕。问题是draw命令无法执行第二次(即在我们重新启动应用程序之后),而所有其他功能都能正常工作(如果我们点击我们放置它的位置,你仍然可以使用我们放在屏幕上的按钮)

public class MenuScreen extends Screen
{
    Texture Background,Playgame,Credits;
    Vector2 PlaygamePos,CreditsPos; 
    Rectangle PlaygameRect,CreditsRect,touchRect;

public MenuScreen() 
{
    Background = GameEngine.LoadTexture("Menu/bg.png");
    GameEngine.BackgroundTexture = Background;

    Playgame = GameEngine.LoadTexture("Menu/newgame.png");
    Credits = GameEngine.LoadTexture("Menu/credits.png");

    PlaygamePos = new Vector2(500,300);
    CreditsPos = new Vector2(500,200);

    PlaygameRect = new Rectangle(PlaygamePos.x,PlaygamePos.y,Playgame.getWidth(),Playgame.getHeight());
    CreditsRect = new Rectangle(CreditsPos.x,CreditsPos.y,Credits.getWidth(),Credits.getHeight());
}

@Override
public void Update(OrthographicCamera camera)
{
    if(GameEngine.isBackButton == true)
    {
        GameEngine.isBackButton = false;
        Gdx.app.exit();
    }

    if(GameEngine.isTouched() == true)
    {
        Vector3 touchPos = new Vector3();
        touchPos = GameEngine.TouchPos(camera);

        touchRect = new Rectangle(touchPos.x,touchPos.y,10,10);

        if(touchRect.overlaps(PlaygameRect))
        {
             SelectScreen = ScreenSelect.GamePlay;
        }
        else if(touchRect.overlaps(CreditsRect))
        {

        }
    }
}

@Override
public void Draw(SpriteBatch spriteBatch) 
{
    spriteBatch.draw(Credits,CreditsPos.x,CreditsPos.y);
    spriteBatch.draw(Playgame,PlaygamePos.x,PlaygamePos.y);
}

@Override
public void Resume() {
    // TODO Auto-generated method stub

}

@Override
public void Pause() {
    // TODO Auto-generated method stub

}

@Override
public void Dispose()
{

}

}

Even we tried Disposing textures and SpriteBatches none worked!

即使我们尝试处理纹理和SpriteBatches也没有用!

1 个解决方案

#1


0  

Most likely you are running into a mismatch between the lifetime of Java objects (tied to the life of the application process) and the lifetime of texture objects (tied to the life of the OpenGL context which is tied to the visibility of the Activity). On app "restart", I suspect you're actually just exiting the Activity, and Android is caching the process and starting a new Activity in the same process when you "re-start" the you app. In this case you'll have a valid Java Texture object, but the underlying bytes it "points to" in the OpenGL context are gone (since the OpenGL context is invalidated when the Activity is no longer visible).

很可能你遇到了Java对象的生命周期(与应用程序进程的生命周期相关)和纹理对象的生命周期之间的不匹配(与OpenGL上下文的生命周期相关,这与Activity的可见性有关)。在应用程序“重启”时,我怀疑你实际上只是退出了Activity,并且当你“重新启动”你的应用程序时,Android正在缓存该进程并在同一进程中启动一个新的Activity。在这种情况下,您将拥有一个有效的Java Texture对象,但它在OpenGL上下文中“指向”的基础字节已经消失(因为当Activity不再可见时,OpenGL上下文无效)。

The fix is to re-load textures on activity creation. You can either just make sure all your objects that contain textures (and objects that contain objects that contain textures, etc) are tied to the Activity lifecycle.

修复是在活动创建时重新加载纹理。您可以确保包含纹理的所有对象(以及包含包含纹理的对象的对象等)都绑定到Activity生命周期。

#1


0  

Most likely you are running into a mismatch between the lifetime of Java objects (tied to the life of the application process) and the lifetime of texture objects (tied to the life of the OpenGL context which is tied to the visibility of the Activity). On app "restart", I suspect you're actually just exiting the Activity, and Android is caching the process and starting a new Activity in the same process when you "re-start" the you app. In this case you'll have a valid Java Texture object, but the underlying bytes it "points to" in the OpenGL context are gone (since the OpenGL context is invalidated when the Activity is no longer visible).

很可能你遇到了Java对象的生命周期(与应用程序进程的生命周期相关)和纹理对象的生命周期之间的不匹配(与OpenGL上下文的生命周期相关,这与Activity的可见性有关)。在应用程序“重启”时,我怀疑你实际上只是退出了Activity,并且当你“重新启动”你的应用程序时,Android正在缓存该进程并在同一进程中启动一个新的Activity。在这种情况下,您将拥有一个有效的Java Texture对象,但它在OpenGL上下文中“指向”的基础字节已经消失(因为当Activity不再可见时,OpenGL上下文无效)。

The fix is to re-load textures on activity creation. You can either just make sure all your objects that contain textures (and objects that contain objects that contain textures, etc) are tied to the Activity lifecycle.

修复是在活动创建时重新加载纹理。您可以确保包含纹理的所有对象(以及包含包含纹理的对象的对象等)都绑定到Activity生命周期。