O'Reilly logo

Developing Mobile Games with Moai SDK by Francisco Tufro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Camera and scrolling

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 ()
  1. What we're doing here is creating a new camera using the built-in MOAICamera2D class.
  2. 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required