Browse Source

Fixed slider-drag problem in [falling-balls-mpp] example. (#1750)

pull/1757/head
Roman Sedaikin 3 years ago committed by GitHub
parent
commit
b8ec76d166
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      experimental/examples/falling-balls-mpp/src/commonMain/kotlin/fallingBalls/FallingBalls.kt
  2. 6
      experimental/examples/falling-balls-mpp/src/commonMain/kotlin/fallingBalls/Game.kt

7
experimental/examples/falling-balls-mpp/src/commonMain/kotlin/fallingBalls/FallingBalls.kt

@ -14,6 +14,7 @@ import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em import androidx.compose.ui.unit.em
import org.jetbrains.skiko.KotlinBackend import org.jetbrains.skiko.KotlinBackend
import kotlin.math.round
@Composable @Composable
fun FallingBalls(game: Game) { fun FallingBalls(game: Game) {
@ -24,13 +25,13 @@ fun FallingBalls(game: Game) {
fontSize = 1.8f.em, fontSize = 1.8f.em,
color = Color(218, 120, 91) color = Color(218, 120, 91)
) )
Text("Score: ${game.score} Time: ${game.elapsed / 1_000_000} Blocks: ${game.numBlocks}", fontSize = 1.8f.em) Text("Score: ${game.score} Time: ${game.elapsed / 1_000_000} Blocks: ${game.numBlocks.toInt()}", fontSize = 1.8f.em)
Row { Row {
if (!game.started) { if (!game.started) {
Slider( Slider(
value = game.numBlocks / 20f, value = game.numBlocks / 20f,
onValueChange = { game.numBlocks = (it * 20f).toInt().coerceAtLeast(1) }, onValueChange = { game.numBlocks = (it * 20f).coerceAtLeast(1f) },
modifier = Modifier.width(100.dp) modifier = Modifier.width(250.dp)
) )
} }
Button( Button(

6
experimental/examples/falling-balls-mpp/src/commonMain/kotlin/fallingBalls/Game.kt

@ -33,7 +33,7 @@ class Game(val time: Time) {
var paused by mutableStateOf(false) var paused by mutableStateOf(false)
var finished by mutableStateOf(false) var finished by mutableStateOf(false)
var numBlocks by mutableStateOf(5) var numBlocks by mutableStateOf(5f)
fun start() { fun start() {
previousTimeNanos = time.now() previousTimeNanos = time.now()
@ -43,7 +43,7 @@ class Game(val time: Time) {
finished = false finished = false
paused = false paused = false
pieces.clear() pieces.clear()
repeat(numBlocks) { index -> repeat(numBlocks.toInt()) { index ->
pieces.add(PieceData(this, index * 1.5f + 5f, colors[index % colors.size]).also { piece -> pieces.add(PieceData(this, index * 1.5f + 5f, colors[index % colors.size]).also { piece ->
piece.position = Random.nextDouble(0.0, 100.0).toFloat() piece.position = Random.nextDouble(0.0, 100.0).toFloat()
}) })
@ -65,7 +65,7 @@ class Game(val time: Time) {
fun clicked(piece: PieceData) { fun clicked(piece: PieceData) {
score += piece.velocity.toInt() score += piece.velocity.toInt()
clicked++ clicked++
if (clicked == numBlocks) { if (clicked == numBlocks.toInt()) {
finished = true finished = true
} }
} }

Loading…
Cancel
Save