Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

there is a crash about 'lateinit property ticker has not been initialized' in GameButton.kt #6

Open
ZMCurry opened this issue Apr 27, 2022 · 4 comments

Comments

@ZMCurry
Copy link

ZMCurry commented Apr 27, 2022

After starting the game, long pressing the down arrow keys will cause a crash.
lateinit var ticker needs to be promoted to a member variable

@Miladt
Copy link

Miladt commented Jan 29, 2023

i'm having the same issue

@BaTM1234
Copy link

Can someone help fix this?

@longunidev
Copy link

I'm having this issue too. Anyone here has a fix?

@mylab4492
Copy link

Try this one


@Composable
fun GameButton(
    modifier: Modifier = Modifier,
    size: Dp,
    onClick: () -> Unit = {},
    autoInvokeWhenPressed: Boolean = false,
    content: @Composable (Modifier) -> Unit = {},
) {
    val backgroundShape = RoundedCornerShape(size / 2)
    var isPressed by remember { mutableStateOf(false) }

    val coroutineScope = rememberCoroutineScope()

    Box(
        modifier = modifier
            .background(
                brush = Brush.verticalGradient(
                    colors = listOf(
                        Purple200,
                        Purple500
                    )
                ),
                shape = backgroundShape
            )
            .pointerInput(autoInvokeWhenPressed) {
                detectTapGestures(
                    onPress = {
                        isPressed = true
                        if (autoInvokeWhenPressed) {
                            // Start the coroutine for auto-clicking
                            coroutineScope.launch {
                                delay(300)  // Initial delay before the first click
                                while (isPressed) {
                                    onClick()  // Trigger the click event
                                    delay(60)  // Delay between repeated clicks
                                }
                            }
                        }
                        tryAwaitRelease() // Wait for the release
                        isPressed = false  // Reset the pressed state
                        onClick()  // Trigger the final click on release
                    }
                )
            }
            .size(size)
    ) {
        content(Modifier.align(Alignment.Center))
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants