Corona – Movie Clips

Often you will want to create a short animated clip. This is good for a lots of things especially games. A movieClip is an object that contains a series of images. Movieclips can play display their images in succession to create animated effect.

MovieClips are really a series of images loaded into to Corona and played in sequence. You could accomplish this with a Group containing all of the images and an array. Then writing an enterFrame function to cycle through the images in the array making each one visible in turn.

You could do all of that but, you don’t need to since someone over at Corona has all ready done it for you! This example uses the movieclip.lua file. You can find this in CoronaSDK/SampleCode/Graphics/MovieClip.

Create a new Corona project.

  1. Create a folder to work in
  2. Copy the movieclip.lua file from CoronaSDK/SampleCode/Graphics/MovieClip into the folder where you will be working
  3. Create a new text file and save it into the folder an name it main.lua
  4. Create a series of small images to use for the animated sequence. Imagine these images as frames in a movie. Save the images into the project folder

Add the following code to main.lua

-- Import an external library
local movieclip = require("movieclip")

-- Since we're importing a library we need to make a main function
-- to run after the library has loaded

-- Define the main function to initialize this program
function main()
 -- This line makes a new animated "movieClip" from the saucer images in the table
 saucer = movieclip.newAnim{"saucer_01.png", "saucer_02.png", "saucer_03.png"}

 saucer.x = display.contentWidth / 2
 saucer.y = display.contentHeight / 2

 -- Start the animation playing
 saucer:play()
end 

-- Call main to start the program
main()

Corona – frame based animation

Transitions work well for situations when you want a motion to run from start to finish with a set length of time. For example you’d like to have en element move onto the page and stop in the center over 3 seconds. At other times you’ll want to create a motion that plays continuously. For example, you have object that moves back and forth across the screen for as long as it is on the screen. In these cases transition.to() doesn’t work so well. For these situations we can use the “enterFrame” event.

The enterFrame event

The enterFrame event is an event that us dispatched once every frame. Which of course brings up the idea of frames. Think of frames as the number of times Corona updates the screen. This is typically 30 times per second. So the enterFrame event is an event that is sent 30 times per second.

You can use the enterFrame event to do lots of things. A common purpose is animation. Imagine you had a object on the screen you added 12 degrees to it’s rotation 30 times each second. It would make a complete rotation every second.

Test the enterFrame event

Create a new Corona project

  1. Make a folder to work in
  2. Create a new text file and save into the folder with the name main.lua
  3. Copy a small image into your folder

In your main.lua file add the following code.

-- Make a new image
local invader = display.newImage("invader_04.png")

-- Position the image at a starting location off the left edge of the screen
invader.x = -80
invader.y = 160

-- Use this function move the image, this function will be run once each frame
-- each frame add 5 to the x. If x is greater than 400 move the image to the left
local function move(event)
 invader.x = invader.x + 5
 if invader.x > 400 then
 invader.x = -80
 end
end

-- Add an enterFrame event to the Runtime object
Runtime:addEventListener( "enterFrame", move )

Corona – Events

Think of notifications from the program about things that have happened in the program. Use events to determine when an object is tapped or when period of time has passed. Use events to animate things and react to user interaction.

Listeners and Handlers

Listeners are objects that have been assigned to respond to an event. Handlers are the functions that are run when an event occurs. Think of listeners as waiting for and notifying their handlers when the event occurs.

Most objects in Corona can be assigned a listener. Use the addEventListener() method to assign the listener. For example imagine you want a create an event is fired when you tap an image. You might write the following. Try it for yourself.

  1. Create a new folder to work in
  2. Open a new text file and save it to your folder with the name main.lua
  3. Copy an image to use in the example to your folder

Add the following code to main.lua:

-- Create an image and position it in the center of the screen
local invader = display.newImage("invader_04.png")

invader.x = display.contentWidth / 2
invader.y = display.contentHeight / 2

-- Define a handler
local function tapInvader( event )
 transition.to(invader, {time=1000, rotation=invader.rotation+360})
end 

-- Add an event listener to the invader
invader:addEventListener("tap", tapInvader)

I added a few comments to the code to explain what is going on. Any line that begins with “–” is ignored and the rest of the line can be used to write a message or description of your code.

Test your project and tap the invader. With each tap it rotates 360!

 

Corona – Groups

At some point you will want to work with groups of display objects. Corona provides a special type of Group display object.

Try out groups for yourself. Start by making a new Corona project.

  1. Create a folder to work in
  2. Create a new text file, save it to your folder named main.lua
  3. Add a few small images to your folder

First create three images.

local square = display.newImage("square.png")
local circle = display.newImage("circle.png")
local hexagon = display.newImage("hexagon.png")

 

Next create a group.

local group = display.newGroup()

Now add the objects to group

group:insert( square )
group:insert( circle )
group:insert( hexagon )

 

Now we’ll off set the images a little. Here placed circle near the upper right corner of square and hexagon near the lower left corner of square.

square.x = 0
square.y = 0
circle.x = 50
circle.y = -50
hexagon.x = -50
hexagon.y = 50

Now try transforming the group. Notice all of the images transform as a group. Placing the group in the center of the screen moves all of the images there. Rotating the group rotates all of the images. All of the transformations are based on the group’s reference point.

group.x = 160
group.y = 240
group.rotation = 5
group.alpha = .5

You can also animate the group. Try the following.

transition.to( group, {time=3000, rotation=360, transition=easing.inOutExpo})

A group acts as a display object in every way, with the difference that Groups can contain other display objects including other groups.

 

 

 

Corona – Stacking Objects

Once you’ve created more than one object the order in which they stack will be come important. What I mean by “stacking order” is which object is in front and which is behind, imagine layers in Photoshop.

Corona stacks or layers objects in the order that they are created. In other words the first object created would be on the bottom. While each newer object created would stack on top of the previous.

Try this out for yourself. Create a new Corona project.

  1. Make a new Folder to work in.
  2. Make a new text file, save it to the folder as main.lua.
  3. Add three small images to your folder.

Create three images in your Corona project. Use the following to load your images. In my example the images were named square.png, circle.png and hexagon.png.

local square = display.newImage("square.png")
local circle = display.newImage("circle.png")
local hexagon = display.newImage("hexagon.png")

Notice the square is behind the other images. The circle “stacks” or looks like it’s on a layer between the other two. And, the hexagon is on top of the others.

Note: If any of the upper are larger than the images below, the images may not be visible.

The stacking order is determined by the order the images were created. With newer images being placed on top of images that were created earlier.

Corona provides a few methods that allow you to manipulate the stacking order. To move an object to the front use object:toFron(). For example, the following would move the circle to the front.

circle:toFront()

Use the object:toBack() method to move an object to the back. For example, the following moves the hexagon to the back.

hexagon:toBack()