What is the right way to use Camera and CameraCont...
# korge
v
What is the right way to use Camera and CameraContainer? I am making a platformer, and when the character is close to the edge of the screen, the view point should move and reveal more of the game scene. For now all my game objects are children of a Scene.
m
I use it like this, i think its the official way:
Copy code
val cameraBounds = ClipContainer(1000.0, 1000.0)
        val camera = Camera()
        cameraBounds.addChild(camera)
        camera.addChild(gameContainer)
        stage.addChild(cameraBounds)
and
gameContainer
is a Container holding all world game objects
v
Could you show the part of your code that moves the camera view?
m
Created this extention function for Camera to make it work like i wanted to. There might be better ways of doing the same thing.
Copy code
fun Camera.setTo(x:Double, y:Double, zoom: Double){
    val stage = this.stage!!
    this.setTo(
        Rectangle(
            stage.x + x * stage.scaleX - stage.scaledWidth / 2 * zoom,
            stage.y + y * stage.scaleY - stage.scaledHeight / 2 * zoom,
            stage.scaledWidth * zoom,
            stage.scaledWidth * zoom,
        )
    )
}
then i just do:
Copy code
var zoom = 1.0
...
stage.addUpdater {
    ...
    camera.setTo(localPlayer.container.x, localPlayer.container.y, zoom)
}
to center the camera on the player, Seems to work well 👍
v
Cool, thank you!