在我的游戏中,我有这个代码。 它呈现一个用作按钮的纹理:
private void drawStart(){ startTexture = new Texture(Gdx.files.internal("start.png")); startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); stageStart = new Stage(); stageStart.clear(); buttonStart = new Image(startTexture); buttonStart.setX(10); buttonStart.setY(Gdx.graphics.getHeight()/2.75f); buttonStart.setWidth(Gdx.graphics.getWidth()/4); buttonStart.setHeight(Gdx.graphics.getHeight()/4); Gdx.input.setInputProcessor(stageStart); buttonStart.addListener(new ClickListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { currentState = GameState.RESET; startTexture.dispose(); stageStart.dispose(); return true; } }); stageStart.addActor(buttonStart); stageStart.draw(); startTexture.dispose();}
但是,每当我把drawStart(); 在我的渲染方法中,Java堆和原生堆每10秒缓慢增加1。 因此,如果用户在菜单上离开游戏约5分钟,游戏将在他们的手机上崩溃。 我测试了它,它只在渲染纹理时才会出现。
我很感激任何帮助解决这个问题。 我已经尝试了一个if语句,表明如果render = 0,渲染纹理然后设置渲染1但是没有用。
Inside my game I have this code. It renders a texture that serve as a button:
private void drawStart(){ startTexture = new Texture(Gdx.files.internal("start.png")); startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); stageStart = new Stage(); stageStart.clear(); buttonStart = new Image(startTexture); buttonStart.setX(10); buttonStart.setY(Gdx.graphics.getHeight()/2.75f); buttonStart.setWidth(Gdx.graphics.getWidth()/4); buttonStart.setHeight(Gdx.graphics.getHeight()/4); Gdx.input.setInputProcessor(stageStart); buttonStart.addListener(new ClickListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { currentState = GameState.RESET; startTexture.dispose(); stageStart.dispose(); return true; } }); stageStart.addActor(buttonStart); stageStart.draw(); startTexture.dispose();}
However, whenever I put drawStart(); into my render method, the Java Heap and Native Heap slowly increases by 1 every 10 seconds. So, if the user leaves the game on the menu for about 5 minutes the game will crash on their phone. I've tested it and it only occurs when the texture is rendered.
I would appreciate any help on fixing this. I have tried an if statement that states if rendered = 0, render the texture then set rendered 1 but that didn't work.
最满意答案
这可能对你有帮助。 您只需要在渲染中绘制。 所以现在你可以在你的渲染方法中放置drawStart(),它只会绘制舞台,同时让屏幕不要忘记调用dispose。
private void drawStart(){ stageStart.draw(); } public void initialize() { startTexture = new Texture(Gdx.files.internal("start.png")); startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); stageStart = new Stage(); stageStart.clear(); buttonStart = new Image(startTexture); buttonStart.setX(10); buttonStart.setY(Gdx.graphics.getHeight()/2.75f); buttonStart.setWidth(Gdx.graphics.getWidth()/4); buttonStart.setHeight(Gdx.graphics.getHeight()/4); Gdx.input.setInputProcessor(stageStart); buttonStart.addListener(new ClickListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { currentState = GameState.RESET; startTexture.dispose(); stageStart.dispose(); return true; } }); stageStart.addActor(buttonStart); } public void dispose() { startTexture.dispose(); }This might help you. You only need draw in your render. So now you can put drawStart() in your render method which will only draw the stage, while leaving screen dont forget to call dispose.
private void drawStart(){ stageStart.draw(); } public void initialize() { startTexture = new Texture(Gdx.files.internal("start.png")); startTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); stageStart = new Stage(); stageStart.clear(); buttonStart = new Image(startTexture); buttonStart.setX(10); buttonStart.setY(Gdx.graphics.getHeight()/2.75f); buttonStart.setWidth(Gdx.graphics.getWidth()/4); buttonStart.setHeight(Gdx.graphics.getHeight()/4); Gdx.input.setInputProcessor(stageStart); buttonStart.addListener(new ClickListener() { public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { currentState = GameState.RESET; startTexture.dispose(); stageStart.dispose(); return true; } }); stageStart.addActor(buttonStart); } public void dispose() { startTexture.dispose(); }更多推荐
发布评论