diff --git a/examples/falling_balls/src/main/kotlin/Game.kt b/examples/falling_balls/src/main/kotlin/Game.kt index e1c32cb418..ade43f44ef 100644 --- a/examples/falling_balls/src/main/kotlin/Game.kt +++ b/examples/falling_balls/src/main/kotlin/Game.kt @@ -8,9 +8,8 @@ import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.onSizeChanged -import androidx.compose.ui.unit.IntSize -import androidx.compose.ui.unit.dp -import androidx.compose.ui.unit.sp +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.unit.* import kotlin.random.Random class Game { @@ -21,7 +20,7 @@ class Game { ) private var startTime = 0L - var size by mutableStateOf(IntSize(0, 0)) + var size by mutableStateOf(Pair(0.dp, 0.dp)) var pieces = mutableStateListOf() private set @@ -75,6 +74,7 @@ class Game { @Composable fun FallingBallsGame() { val game = remember { Game() } + val density = LocalDensity.current Column { Text( "Catch balls!${if (game.finished) " Game over!" else ""}", @@ -110,9 +110,11 @@ fun FallingBallsGame() { if (game.started) { Box(modifier = Modifier .fillMaxWidth() - .fillMaxHeight(0.5f) + .fillMaxHeight() .onSizeChanged { - game.size = it + with(density) { + game.size = it.width.toDp() to it.height.toDp() + } } ) { game.pieces.forEachIndexed { index, piece -> Piece(index, piece) } diff --git a/examples/falling_balls/src/main/kotlin/Piece.kt b/examples/falling_balls/src/main/kotlin/Piece.kt index c8bd3d4c50..1ded6905b8 100644 --- a/examples/falling_balls/src/main/kotlin/Piece.kt +++ b/examples/falling_balls/src/main/kotlin/Piece.kt @@ -41,7 +41,7 @@ data class PieceData(val game: Game, val velocity: Float, val color: Color) { fun update(dt: Long) { if (clicked) return val delta = (dt / 1E8 * velocity).toFloat() - position = if (position < game.size.height) position + delta else 0f + position = if (position < game.size.second.value) position + delta else 0f } fun click() {