Can not stop render previos Screen1() after setScreen(new Screen2());










0















I have two identical classes: Screen1 and Screen2. The problem is that when I open Screen2 from Screen1 the animated sprite of Screen1 continues to render. I see it in the log. And after a while memory overflow occurs. How to stop rendering a closed screen?



public class Screen1 extends Game implements Screen, InputProcessor 

private SpriteBatch batch;
private TextureAtlas atlasBarrelBlue;
private Sprite sprite1;
private float sprite1positionX;
private float sprite1positionY;
private float spriteBarrelWidth;
private float spriteBarrelHeight;
private Texture backgroundTexture;
private Animation<TextureAtlas.AtlasRegion> animationBarrelBlue;
private float dTime = 0;
private Vector2 vector2;

@Override
public void show()

batch = new SpriteBatch();
vector2 = new Vector2();
atlasBarrelBlue = new TextureAtlas(Gdx.files.internal("data/barrels/barr4"));
animationBarrelBlue = new Animation<TextureAtlas.AtlasRegion>(1 / 24f, atlasBarrelBlue.getRegions(), Animation.PlayMode.LOOP);

spriteBarrelWidth = Gdx.graphics.getWidth() / 50 * 4;
spriteBarrelHeight = Gdx.graphics.getHeight() / 50 * 11;
backgroundTexture = new Texture("data/fones/fone1.png");

sprite1positionX = Gdx.graphics.getWidth() / 50 * 7;
sprite1positionY = Gdx.graphics.getHeight() / 50 * 32;

Gdx.input.setInputProcessor(this);



@Override
public void render(float delta)

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

dTime += Gdx.graphics.getDeltaTime();

sprite1 = new Sprite(animationBarrelBlue.getKeyFrame(dTime, true));
Gdx.app.log("111", "111");
sprite1.setPosition(sprite1positionX, sprite1positionY);
sprite1.setSize(spriteBarrelWidth, spriteBarrelHeight);

batch.begin();
batch.draw(backgroundTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
sprite1.draw(batch);
batch.end();

super.render();


@Override
public void dispose()
batch.dispose();
atlasBarrelBlue.dispose();
backgroundTexture.dispose();


@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)

vector2.set(screenX, Gdx.graphics.getHeight() - screenY);
if (sprite1.getBoundingRectangle().contains(vector2))
setScreen(new Screen2());


return false;




Dyspose() and hide() methods do not help. When i go from Screen2 to Screen1 the same thing happens.










share|improve this question
























  • Where is your create() method? When you extend Game you need a create() method

    – Morchul
    Nov 13 '18 at 8:45















0















I have two identical classes: Screen1 and Screen2. The problem is that when I open Screen2 from Screen1 the animated sprite of Screen1 continues to render. I see it in the log. And after a while memory overflow occurs. How to stop rendering a closed screen?



public class Screen1 extends Game implements Screen, InputProcessor 

private SpriteBatch batch;
private TextureAtlas atlasBarrelBlue;
private Sprite sprite1;
private float sprite1positionX;
private float sprite1positionY;
private float spriteBarrelWidth;
private float spriteBarrelHeight;
private Texture backgroundTexture;
private Animation<TextureAtlas.AtlasRegion> animationBarrelBlue;
private float dTime = 0;
private Vector2 vector2;

@Override
public void show()

batch = new SpriteBatch();
vector2 = new Vector2();
atlasBarrelBlue = new TextureAtlas(Gdx.files.internal("data/barrels/barr4"));
animationBarrelBlue = new Animation<TextureAtlas.AtlasRegion>(1 / 24f, atlasBarrelBlue.getRegions(), Animation.PlayMode.LOOP);

spriteBarrelWidth = Gdx.graphics.getWidth() / 50 * 4;
spriteBarrelHeight = Gdx.graphics.getHeight() / 50 * 11;
backgroundTexture = new Texture("data/fones/fone1.png");

sprite1positionX = Gdx.graphics.getWidth() / 50 * 7;
sprite1positionY = Gdx.graphics.getHeight() / 50 * 32;

Gdx.input.setInputProcessor(this);



@Override
public void render(float delta)

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

dTime += Gdx.graphics.getDeltaTime();

sprite1 = new Sprite(animationBarrelBlue.getKeyFrame(dTime, true));
Gdx.app.log("111", "111");
sprite1.setPosition(sprite1positionX, sprite1positionY);
sprite1.setSize(spriteBarrelWidth, spriteBarrelHeight);

batch.begin();
batch.draw(backgroundTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
sprite1.draw(batch);
batch.end();

super.render();


@Override
public void dispose()
batch.dispose();
atlasBarrelBlue.dispose();
backgroundTexture.dispose();


@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)

vector2.set(screenX, Gdx.graphics.getHeight() - screenY);
if (sprite1.getBoundingRectangle().contains(vector2))
setScreen(new Screen2());


return false;




Dyspose() and hide() methods do not help. When i go from Screen2 to Screen1 the same thing happens.










share|improve this question
























  • Where is your create() method? When you extend Game you need a create() method

    – Morchul
    Nov 13 '18 at 8:45













0












0








0








I have two identical classes: Screen1 and Screen2. The problem is that when I open Screen2 from Screen1 the animated sprite of Screen1 continues to render. I see it in the log. And after a while memory overflow occurs. How to stop rendering a closed screen?



public class Screen1 extends Game implements Screen, InputProcessor 

private SpriteBatch batch;
private TextureAtlas atlasBarrelBlue;
private Sprite sprite1;
private float sprite1positionX;
private float sprite1positionY;
private float spriteBarrelWidth;
private float spriteBarrelHeight;
private Texture backgroundTexture;
private Animation<TextureAtlas.AtlasRegion> animationBarrelBlue;
private float dTime = 0;
private Vector2 vector2;

@Override
public void show()

batch = new SpriteBatch();
vector2 = new Vector2();
atlasBarrelBlue = new TextureAtlas(Gdx.files.internal("data/barrels/barr4"));
animationBarrelBlue = new Animation<TextureAtlas.AtlasRegion>(1 / 24f, atlasBarrelBlue.getRegions(), Animation.PlayMode.LOOP);

spriteBarrelWidth = Gdx.graphics.getWidth() / 50 * 4;
spriteBarrelHeight = Gdx.graphics.getHeight() / 50 * 11;
backgroundTexture = new Texture("data/fones/fone1.png");

sprite1positionX = Gdx.graphics.getWidth() / 50 * 7;
sprite1positionY = Gdx.graphics.getHeight() / 50 * 32;

Gdx.input.setInputProcessor(this);



@Override
public void render(float delta)

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

dTime += Gdx.graphics.getDeltaTime();

sprite1 = new Sprite(animationBarrelBlue.getKeyFrame(dTime, true));
Gdx.app.log("111", "111");
sprite1.setPosition(sprite1positionX, sprite1positionY);
sprite1.setSize(spriteBarrelWidth, spriteBarrelHeight);

batch.begin();
batch.draw(backgroundTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
sprite1.draw(batch);
batch.end();

super.render();


@Override
public void dispose()
batch.dispose();
atlasBarrelBlue.dispose();
backgroundTexture.dispose();


@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)

vector2.set(screenX, Gdx.graphics.getHeight() - screenY);
if (sprite1.getBoundingRectangle().contains(vector2))
setScreen(new Screen2());


return false;




Dyspose() and hide() methods do not help. When i go from Screen2 to Screen1 the same thing happens.










share|improve this question
















I have two identical classes: Screen1 and Screen2. The problem is that when I open Screen2 from Screen1 the animated sprite of Screen1 continues to render. I see it in the log. And after a while memory overflow occurs. How to stop rendering a closed screen?



public class Screen1 extends Game implements Screen, InputProcessor 

private SpriteBatch batch;
private TextureAtlas atlasBarrelBlue;
private Sprite sprite1;
private float sprite1positionX;
private float sprite1positionY;
private float spriteBarrelWidth;
private float spriteBarrelHeight;
private Texture backgroundTexture;
private Animation<TextureAtlas.AtlasRegion> animationBarrelBlue;
private float dTime = 0;
private Vector2 vector2;

@Override
public void show()

batch = new SpriteBatch();
vector2 = new Vector2();
atlasBarrelBlue = new TextureAtlas(Gdx.files.internal("data/barrels/barr4"));
animationBarrelBlue = new Animation<TextureAtlas.AtlasRegion>(1 / 24f, atlasBarrelBlue.getRegions(), Animation.PlayMode.LOOP);

spriteBarrelWidth = Gdx.graphics.getWidth() / 50 * 4;
spriteBarrelHeight = Gdx.graphics.getHeight() / 50 * 11;
backgroundTexture = new Texture("data/fones/fone1.png");

sprite1positionX = Gdx.graphics.getWidth() / 50 * 7;
sprite1positionY = Gdx.graphics.getHeight() / 50 * 32;

Gdx.input.setInputProcessor(this);



@Override
public void render(float delta)

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

dTime += Gdx.graphics.getDeltaTime();

sprite1 = new Sprite(animationBarrelBlue.getKeyFrame(dTime, true));
Gdx.app.log("111", "111");
sprite1.setPosition(sprite1positionX, sprite1positionY);
sprite1.setSize(spriteBarrelWidth, spriteBarrelHeight);

batch.begin();
batch.draw(backgroundTexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
sprite1.draw(batch);
batch.end();

super.render();


@Override
public void dispose()
batch.dispose();
atlasBarrelBlue.dispose();
backgroundTexture.dispose();


@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button)

vector2.set(screenX, Gdx.graphics.getHeight() - screenY);
if (sprite1.getBoundingRectangle().contains(vector2))
setScreen(new Screen2());


return false;




Dyspose() and hide() methods do not help. When i go from Screen2 to Screen1 the same thing happens.







java libgdx






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 9:27









Morchul

1,103113




1,103113










asked Nov 12 '18 at 21:44









RetronRetron

31




31












  • Where is your create() method? When you extend Game you need a create() method

    – Morchul
    Nov 13 '18 at 8:45

















  • Where is your create() method? When you extend Game you need a create() method

    – Morchul
    Nov 13 '18 at 8:45
















Where is your create() method? When you extend Game you need a create() method

– Morchul
Nov 13 '18 at 8:45





Where is your create() method? When you extend Game you need a create() method

– Morchul
Nov 13 '18 at 8:45












1 Answer
1






active

oldest

votes


















0














Your problem is that you extend Game, implements Screen and call super.update().



libgdx will call the render method of the Game class.

While the Game class holds a screen and call the render method of the current screen you set with setScreen().



When we look at the render method of the Game class:



@Override
public void render ()
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());



screen is the screen you have set with setScreen()



So Libgdx call => Game.render() and Game.render() call => Screen.render(float delta)




Now when we look on your code. You override the method: render(float delta) which is the render method of the Screen. (The render method of the Game has no parameter).



So first libgdx call Game.render(); this calls the render method of Screen1 and at the end of your overridden Screen1.render(float delta); method, you call again the Game.render() function with super.render();.

So you recursively call the Screen1.render(float delta); method. This is the reason why you become a memory overflow.



I will recommend you to not extend Game and implement Screen in the same class
The class which will be created in the DesktopLauncher should extend Game.

In my case TextAdventure:



public class TextAdventure extends Game 

@Override
public void create ()
setScreen(new Screen1(this));


@Override
public void render()
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

super.render();




And in the Screen1 class only implements Screen and don't call super.render()!:



public class Screen1 implements Screen 

Game myGame;

public Screen1(Game myGame)
this.myGame = myGame;


public void changeToScreen2()
myGame.setScreen(new Screen2(myGame));


...



To change the Screen in Screen1 pass the Game class in the Constructor.






share|improve this answer























  • Thank you very much. It works!

    – Retron
    Nov 13 '18 at 9:55










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270537%2fcan-not-stop-render-previos-screen1-after-setscreennew-screen2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Your problem is that you extend Game, implements Screen and call super.update().



libgdx will call the render method of the Game class.

While the Game class holds a screen and call the render method of the current screen you set with setScreen().



When we look at the render method of the Game class:



@Override
public void render ()
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());



screen is the screen you have set with setScreen()



So Libgdx call => Game.render() and Game.render() call => Screen.render(float delta)




Now when we look on your code. You override the method: render(float delta) which is the render method of the Screen. (The render method of the Game has no parameter).



So first libgdx call Game.render(); this calls the render method of Screen1 and at the end of your overridden Screen1.render(float delta); method, you call again the Game.render() function with super.render();.

So you recursively call the Screen1.render(float delta); method. This is the reason why you become a memory overflow.



I will recommend you to not extend Game and implement Screen in the same class
The class which will be created in the DesktopLauncher should extend Game.

In my case TextAdventure:



public class TextAdventure extends Game 

@Override
public void create ()
setScreen(new Screen1(this));


@Override
public void render()
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

super.render();




And in the Screen1 class only implements Screen and don't call super.render()!:



public class Screen1 implements Screen 

Game myGame;

public Screen1(Game myGame)
this.myGame = myGame;


public void changeToScreen2()
myGame.setScreen(new Screen2(myGame));


...



To change the Screen in Screen1 pass the Game class in the Constructor.






share|improve this answer























  • Thank you very much. It works!

    – Retron
    Nov 13 '18 at 9:55















0














Your problem is that you extend Game, implements Screen and call super.update().



libgdx will call the render method of the Game class.

While the Game class holds a screen and call the render method of the current screen you set with setScreen().



When we look at the render method of the Game class:



@Override
public void render ()
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());



screen is the screen you have set with setScreen()



So Libgdx call => Game.render() and Game.render() call => Screen.render(float delta)




Now when we look on your code. You override the method: render(float delta) which is the render method of the Screen. (The render method of the Game has no parameter).



So first libgdx call Game.render(); this calls the render method of Screen1 and at the end of your overridden Screen1.render(float delta); method, you call again the Game.render() function with super.render();.

So you recursively call the Screen1.render(float delta); method. This is the reason why you become a memory overflow.



I will recommend you to not extend Game and implement Screen in the same class
The class which will be created in the DesktopLauncher should extend Game.

In my case TextAdventure:



public class TextAdventure extends Game 

@Override
public void create ()
setScreen(new Screen1(this));


@Override
public void render()
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

super.render();




And in the Screen1 class only implements Screen and don't call super.render()!:



public class Screen1 implements Screen 

Game myGame;

public Screen1(Game myGame)
this.myGame = myGame;


public void changeToScreen2()
myGame.setScreen(new Screen2(myGame));


...



To change the Screen in Screen1 pass the Game class in the Constructor.






share|improve this answer























  • Thank you very much. It works!

    – Retron
    Nov 13 '18 at 9:55













0












0








0







Your problem is that you extend Game, implements Screen and call super.update().



libgdx will call the render method of the Game class.

While the Game class holds a screen and call the render method of the current screen you set with setScreen().



When we look at the render method of the Game class:



@Override
public void render ()
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());



screen is the screen you have set with setScreen()



So Libgdx call => Game.render() and Game.render() call => Screen.render(float delta)




Now when we look on your code. You override the method: render(float delta) which is the render method of the Screen. (The render method of the Game has no parameter).



So first libgdx call Game.render(); this calls the render method of Screen1 and at the end of your overridden Screen1.render(float delta); method, you call again the Game.render() function with super.render();.

So you recursively call the Screen1.render(float delta); method. This is the reason why you become a memory overflow.



I will recommend you to not extend Game and implement Screen in the same class
The class which will be created in the DesktopLauncher should extend Game.

In my case TextAdventure:



public class TextAdventure extends Game 

@Override
public void create ()
setScreen(new Screen1(this));


@Override
public void render()
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

super.render();




And in the Screen1 class only implements Screen and don't call super.render()!:



public class Screen1 implements Screen 

Game myGame;

public Screen1(Game myGame)
this.myGame = myGame;


public void changeToScreen2()
myGame.setScreen(new Screen2(myGame));


...



To change the Screen in Screen1 pass the Game class in the Constructor.






share|improve this answer













Your problem is that you extend Game, implements Screen and call super.update().



libgdx will call the render method of the Game class.

While the Game class holds a screen and call the render method of the current screen you set with setScreen().



When we look at the render method of the Game class:



@Override
public void render ()
if (screen != null) screen.render(Gdx.graphics.getDeltaTime());



screen is the screen you have set with setScreen()



So Libgdx call => Game.render() and Game.render() call => Screen.render(float delta)




Now when we look on your code. You override the method: render(float delta) which is the render method of the Screen. (The render method of the Game has no parameter).



So first libgdx call Game.render(); this calls the render method of Screen1 and at the end of your overridden Screen1.render(float delta); method, you call again the Game.render() function with super.render();.

So you recursively call the Screen1.render(float delta); method. This is the reason why you become a memory overflow.



I will recommend you to not extend Game and implement Screen in the same class
The class which will be created in the DesktopLauncher should extend Game.

In my case TextAdventure:



public class TextAdventure extends Game 

@Override
public void create ()
setScreen(new Screen1(this));


@Override
public void render()
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

super.render();




And in the Screen1 class only implements Screen and don't call super.render()!:



public class Screen1 implements Screen 

Game myGame;

public Screen1(Game myGame)
this.myGame = myGame;


public void changeToScreen2()
myGame.setScreen(new Screen2(myGame));


...



To change the Screen in Screen1 pass the Game class in the Constructor.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 9:04









MorchulMorchul

1,103113




1,103113












  • Thank you very much. It works!

    – Retron
    Nov 13 '18 at 9:55

















  • Thank you very much. It works!

    – Retron
    Nov 13 '18 at 9:55
















Thank you very much. It works!

– Retron
Nov 13 '18 at 9:55





Thank you very much. It works!

– Retron
Nov 13 '18 at 9:55

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270537%2fcan-not-stop-render-previos-screen1-after-setscreennew-screen2%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo