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

Character versus the world

Now we want to make the main character interact with our world.

We'll add some new methods to the Character module as follows:

  1. First we create an object to store our body and shapes as follows:
    function Character:initializePhysics ()
        self.physics = {}
  2. Then we add a dynamic body to the physics simulation. This will be the body of our character as follows:
        self.physics.body = 
        PhysicsManager.world:addBody ( MOAIBox2DBody.DYNAMIC )
  3. Now we position the body using the same initial position that we used for our rendered character as follows:
        self.physics.body:setTransform (
        unpack(character_object.position) )
  4. Now we add a shape. We'll use a square that's the same size as the sprite (64 x 64 ). This is obviously far from ideal, because ...

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