Note that there are some explanatory texts on larger screens.

plurals
  1. POJava and libgdx: How can I use this variable in two overridden methods?
    text
    copied!<p>I am having a big problem trying to figure out how to use a variable in one overridden method, and also use it in another. Specifically, I am using libgdx, and want to use the variable <code>map</code> in the create method in the render method.</p> <p>Here is my code:</p> <pre><code>@Override public void create() { batch = new SpriteBatch(); background = new TextureAtlas( Gdx.files.internal("data/background/background.pack"), Gdx.files.internal("data/background/")); bg = background.findRegion("Background"); t = TMXLoader.readTMX("data/world/World.tmx"); ArrayList&lt;Image&gt; map = TMXLoader.drawMap(); camera = new OrthographicCamera(WIDTH, HEIGHT); camera.position.set(WIDTH / 2, HEIGHT / 2, 0); camera.update(); } @Override public void render() { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); BufferedImage image = new BufferedImage(20, 20, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = (Graphics2D) image.getGraphics(); batch.begin(); for (int i = 0; i &lt; WIDTH; i += bg.getRegionWidth()) { for (int j = 0; j &lt; HEIGHT; j += bg.getRegionHeight()) { batch.draw(bg, i, j); } } for (int x = 0; x &lt; t.width; x++) { for (int y = 0; y &lt; t.height; y++) { for (Image tile : map) { float[] scale = getScale(); Image scaledTile = tile.getScaledInstance( (int) (t.tilewidth * scale[0]), (int) (t.tileheight * scale[1]), Image.SCALE_FAST); g2d.drawImage(scaledTile, x, y, null); } } } batch.end(); } </code></pre> <p>As it is, I can't access <code>map</code> in the render method. I can't create my own method, because it would have to be called in each method create and render. I am stuck, and can't figure it out on my own. I have a feeling I could use a get/set to do this, but I'm not quite sure how I'd try to do that...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload