If you're planning to create games in 2D, an important thing to master is how to use a camera, and that's what we'll do now.
Enter this code at the beginning of Game:initialize ()
in game.lua
:
self.camera = MOAICamera2D.new ()
What we're doing here is creating a new camera using the built-in
MOAICamera2D
class.Next, we need to assign that camera to each layer. Below the viewport assignment
(self.layer:setViewport ( viewport ))
, add the following line of code:self.layer:setCamera ( self.camera )
This binds the layer to that camera. And that's it; when you move the camera, you'll see that the viewport will follow, and if you scale the camera up and down, it will zoom in and zoom out accordingly. We'll write a few lines to demonstrate this.
Inside
Game:processInput ()
write the following code:local x, y = InputManager:deltaPosition ()
We are using a method from
InputManager
that returns the difference between the current mouse position and the previous one.self.camera...