mirror of
https://github.com/jlengrand/compose-multiplatform.git
synced 2026-03-10 08:11:20 +00:00
Use local density instead of setting fillMaxHeight to 0.5f (#590)
This commit is contained in:
@@ -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<PieceData>()
|
||||
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) }
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user