1
votes

My problem step-by-step:

1)I have 0 images on the screen.

2) I create 1st texture and put in on the screen. There is my code to create texture:

  fun getPlanetTexture(planetDiameter: Float): Texture {
    val planetImage = FileSystem.getPlanetBodyFor(planet)
    if (!planetImage.exists()) {
      val planetBitmap = getPlanetBitmap(planetDiameter)
      FileSystem.savePlanetBody(planet, planetBitmap)
    }
    val textureFileHandle = AbsoluteFileHandleResolver().resolve(planetImage.toString())
    return Texture(textureFileHandle)
  }

3) Texture appears on the screen, but if draws just like black rectangle.

4) I save the state of my app to save my models in prefs to get same files and textures enxt time I open the application.

5) I clsoe the application

6) I open the applciation

7) TA-DA! My texture is drawing correctly (it is texture of beautiful planet)

8) I add another texture to the screen.

9) My new texture and old texture too becames black rectangles!

What should I do to avoid black rectangles and always get correctly drawing textures?

This is my code I use to draw texture

  override fun draw(sb: SpriteBatch, sr: ShapeRenderer) {
    val x = model.x * bounds.x
    val y = model.y * bounds.y
    sb.draw(body, x - body.width / 2, y - body.height / 2)
  }
1
Don't use statics for Texture references. Android keeps static references between openings of your game because the underlying Application object is probably still alive. But new instances of the game have new OpenGL contexts.Tenfour04
I never use statics at allP. Ilyin

1 Answers

2
votes

After some searching I found solution:

just place texture-reading to this construction:

Gdx.app.postRunnable(new Runnable {
  val texture = Texture(...)
})