https://kotlinlang.org logo
#korge
Title
# korge
v

voddan

05/08/2021, 3:31 PM
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

Max

05/10/2021, 11:29 AM
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

voddan

05/14/2021, 6:38 PM
Could you show the part of your code that moves the camera view?
m

Max

05/17/2021, 6:14 AM
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

voddan

05/17/2021, 12:19 PM
Cool, thank you!
6 Views