У меня есть кусок кода для рендеринга сетки, из которой я буду рендерить лабиринт. Моя функция сборки:
fun buildAsync(): Deferred<RegularMaze> {
return KtxAsync.async(newSingleThreadAsyncContext()) {
addEmptyFields()
enableLeftBordersRender()
enableBottomBordersRender()
regularMazeService.convertFieldsToMaze(fields, colsNo, rowsNo)
}
}
И это выглядит так: ![enter image description here](https://i.stack.imgur.com/KDOen.jpg)
Но когда я двигаюсь addEmptyFields()
до секции async
, она рендерится правильно ![enter image description here](https://i.stack.imgur.com/e4ifg.jpg)
И мой Wall
класс
class Wall (width: Float, height: Float, x: Float = 0F, y: Float = 0F, rotation: Float = 0F) : BasePart() {
private val textureRegion: TextureRegion
private val size: Size = Size(width, height)
private val position: Position = Position(x, y, rotation)
var relatedFieldIndex: Int? = null
var shouldBeDraw = true
init {
inject()
val wallTexture = assetsHelper.getTextureFromAsset(TextureAsset.WALL)
wallTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat)
textureRegion = TextureRegion(wallTexture, 0, 0, size.widthInt, size.heightInt)
setBounds(
position.x,
position.y,
width,
height
)
rotateBy(position.rotation)
}
override fun draw(batch: Batch, parentAlpha: Float) {
super.draw(batch, parentAlpha)
if (shouldBeDraw) {
batch.draw(textureRegion, position.x, position.y, 0F, 0F, size.width, size.height, 1F, 1F, position.rotation)
}
}
class Size (val width: Float, val height: Float) {
val widthInt: Int
get() = ceil(width).toInt()
val heightInt: Int
get() = ceil(height).toInt()
}
data class Position(val x: Float, val y: Float, val rotation: Float)
}
[РЕДАКТИРОВАТЬ / ОБНОВИТЬ]
Я обнаружил кое-что странно, когда я создаю один Wall
без измерений до async
, он начинает работать. «Рабочий» код:
fun buildAsync(): Deferred<RegularMaze> {
Wall(0f,0f) // <---- new line
return KtxAsync.async(newSingleThreadAsyncContext()) {
addEmptyFields()
enableLeftBordersRender()
enableBottomBordersRender()
regularMazeService.convertFieldsToMaze(fields, colsNo, rowsNo)
}
}
Почему?