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

🚑 Lemon Click event issue #40

Open
hyeonhh opened this issue Nov 17, 2023 · 1 comment
Open

🚑 Lemon Click event issue #40

hyeonhh opened this issue Nov 17, 2023 · 1 comment

Comments

@hyeonhh
Copy link

hyeonhh commented Nov 17, 2023

`
@composable
fun makingLemonade(
contentText: String,
modifier: Modifier=Modifier.padding(16.dp)) {

var isClicked by remember { mutableStateOf(1) }
var randomLemon=(2..4).random()
var squeezedLemon by remember { mutableStateOf(randomLemon)
}
var tabCountNow by remember {
    mutableStateOf(0)
}


val imageResource = when (isClicked) {
    1-> R.drawable.lemon_tree
    2->R.drawable.lemon_squeeze
    3->R.drawable.lemon_drink
    else->R.drawable.lemon_restart
}

val textResource = when (isClicked) {
    1 -> R.string.step1_body
    2 ->R.string.step2_body
    3 -> R.string.step3_body
    else -> R.string.step4_body
}

Column(
    modifier = modifier,
    verticalArrangement = Arrangement.Center,
    horizontalAlignment = Alignment.CenterHorizontally

) {
    Image(
        painter = painterResource(id = imageResource),
        contentDescription = contentText,
        contentScale= ContentScale.Crop,
        modifier= modifier
            .clip(RoundedCornerShape(20.dp))
            .background(Color(0xFF92B8B1))
            .clickable {
                if (isClicked == 2) {
                    //todo : tabcount만큼 클릭이 되면
                    if (squeezedLemon == tabCountNow) isClicked = 3
                    else tabCountNow++

                }
                if (isClicked == 4) {
                    isClicked = 1
                } else {
                    isClicked++
                }


            }

    )
  
}

This is how I implemented the code.
First of all, we looked at each process of making lemonade as the number 1,2,3,4 in the isClick variable.
RandomLemon variable randomly generates the number of times a lemon needs to be pressed
This was saved in the squaredzedLemon variable.

The tabCountNow variable has been remembered to save the number of times the current compression has been made.

And on Modifier.Clickable
I implemented the following logic

But just one tap of the execution results and it goes to the next image. What's the problem?

`

@RakhmetKotlin
Copy link

Hi. Its because both of your if conditionals are true. isClicked == 2 true and then else branch in second if conditional also executes (isClicked++). Try this code. See if it works for you

                    if(isClicked != 2) {
                        isClicked++
                        if(isClicked > 4) {
                            isClicked = 1
                            squeezedLemon = (2..4).random()
                        }
                    } else {
                        squeezedLemon--
                        if (squeezedLemon == 0) {
                            isClicked = 3
                        }
                    }

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

2 participants