Time for action – creating the character

The main character was created with a sprite sheet and needs to be set up to view the animation it provides. Other display images that will make an appearance include a cracked egg when a collision to a physical object has been made. To create the character, perform the following steps:

  1. Create a new local function called createChar():
        local createChar = function()
  2. Create the sprite sheet for the main character:
    local sheetData = { width=128, height=128, numFrames=4, sheetContentWidth=256, sheetContentHeight=256 }
    local sheet = graphics.newImageSheet( "charSprite.png", sheetData )
    
        local sequenceData = 
        {
          { name="move", start=1, count=4, time=400 } 
        }
    
        charObject = display.newSprite( sheet, sequenceData )
        charObject:setSequence("move")
        charObject:play()
  3. Set the starting position and physical properties for the main character:
        charObject.x = 240; charObject.y = 250
        physics.addBody( charObject, "static", { density=1.0, bounce=0.4, friction=0.15, shape=panShape } )
        charObject.rotation = 0
        charObject.isHit = false -- When object is not hit
        charObject.myName = "character"
  4. Add in the transition image after the egg has made a collision:
        friedEgg = display.newImageRect( "friedEgg.png", 40, 23 )
        friedEgg.alpha = 1.0
        friedEgg.isVisible = false
        gameGroup:insert( charObject )
        gameGroup:insert( friedEgg )
      end
    Time for action – creating the character

The image sheet being referred to is called sheetData and takes the first 4 frames of animation from "charSprite.png". We created an animation set called "move". Every time "move" is called, it starts the animation from frame 1 and plays 4 frames from the start at 400 milliseconds.

The main display object is called charObject and takes on the characteristics of sheetData. When it calls setSequence("move"), that animation sequence plays when the play() command is executed.

An important change to the physical body of the character is that its main collision point will be directed towards the frying pan used in the animation. Any collision detection on the character's body will not be read. The charObject display object is given a name called "character", which will be used to detect the collision with the falling egg.

We have also placed the fried egg in this function, to prepare it for the collision.

We want to make sure that when an object has interacted with another, an event type occurs right after. At the instance of a postcollision, we can confirm the collision force between two objects. This helps us determine that the object that was destroyed was completed with a set amount of force.

Many of the native Box2D methods have been made into simpler dot properties for display objects. The following examples show that a body, newBody, has been created using one of the constructor methods.