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

Updating cloud state #10

Open
Tarun1001 opened this issue Jun 11, 2024 · 0 comments
Open

Updating cloud state #10

Tarun1001 opened this issue Jun 11, 2024 · 0 comments

Comments

@Tarun1001
Copy link

Tarun1001 commented Jun 11, 2024

Let's say I only want to implement the cloud animation, so I only have the cloud state. If I change the position of the cloud using moveForward, will it update the cloud state? I tried this, but it's not recomposing.

Please let me know if there are any issues with my code.

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

    val deviceMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(deviceMetrics)
    deviceWidthInPixels = deviceMetrics.widthPixels
    distanceBetweenCactus = (deviceWidthInPixels * 0.4f).toInt()

    setContent {
        DinoRunTheme(darkTheme = isSystemInDarkTheme()) {
            val cloudsState by remember {
                mutableStateOf(
                    CloudState(
                        maxClouds = MAX_CLOUDS,
                        speed = CLOUDS_SPEED
                    )
                )
            }
            val cloudsColor = getCloudColor()
            GameScreen(cloudsState)
        }
    }
}
}
@composable
fun GameScreen(cloudsState: CloudState) {
val cloudsColor = getCloudColor()

LaunchedEffect(Unit) {
    while (true) {
        cloudsState.moveForward()
        delay(16)
    }
}



Column(
    modifier = Modifier
        .fillMaxWidth()
        .clickable { }
) {
    Canvas(modifier = Modifier.weight(1f)) {
        Log.d("GameScreen", "GameScreen: " + cloudsState.cloudsList)
        CloudsView(cloudsState, color = cloudsColor)
    }
}
}

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

1 participant