Skip to content

Adding Life Through Animation

ldurniat edited this page Jan 9, 2018 · 4 revisions

Nowadays animations are crucial element in game development. Berry support animated tiles by using Corona animation sequences.

Step 1: Create animation in Tiled

First, to build an animation, you need to select tileset by left clicking on button Edit Tileset located in the bottom right of Tiled and select Tileset >> The Animation Editor. In new window tap tile to which you want attach new animation. Next form sequence of frames by dragging tiles from right to left side of window.

New Animation

Note: Tiled allows one animation per tile.

To use animation in Corona you need a couple more properties set on your tile or object. First of all, you need add property isAnimated to object not a tile. It will simply tell Berry that this object should be animated. The rest of properties are the same as specified in display.newSprite for sequences i.e. time, name, loopCount and loopDirection. You need add it to a tile.

More Properties

If you run your game now you will notice something, your animation no longer plays. That is because you must tell Berry to play your animations, giving you more control over them.

Step 5: Dealing with the new animations

Now that you have your sequences set up you are ready to test them in game and just like the Properties tutorial we now need to get access to our animated objects. Then after we have it we can start playing it.

-- We first need to get access to the layer our objects is on, the name is specified in Tiled
local layer = map:getObjectLayer( 'Object Layer 1' )

-- Make sure we actually have a layer
if layer then

    -- Get all the objects on this layer
    local objects = layer.objects

    -- Loop through our objects
    for i=1, #objects, 1 do
 
        -- Check if the tile is animated (note the capitalization)
        if objects[i].isAnimated then

            -- Store off a copy of the object
            local object = objects[i]

            -- Now finally play it
            object.sprite:play()
        
        end

    end

end

Play Animation