Corona – Audio

Corona makes it easier than anything to get sound into a project. The documentation says:

Sound Formats

  • All platforms support 16-bit uncompressed .wav files.
  • iOS and Mac Simulator support .mp3, .caf, and .aac.
  • Windows Simulator supports .mp3 along with .wav.
  • As of build 269, Android now supports .mp3 and .ogg formats.

So it looks like you can use wav, mp3, caf and aac on IOS. Since I’m focusing on the IOS platform I’ll stick with one of these formats. You can use garage band to generate AAC and MP3 files.

To get a sound to play you need to load the file creating a reference to the audio object in the process. Then tell that audio object to play.

Try it for yourself. Create a new Corona project, follow these steps:

  1. Create a folder to store your project.
  2. Create a new text file and save it into your folder with the name main.lua.
  3. Find a suitable sound file and copy it to the folder.

Add the following Lua code to your main.lua file. The file I for the example was named beep.mp3. Be sure to replace this name with the name of your sound file.

local mySound = audio.loadSound( "beep.mp3" )
audio.play( mySound )

Pretty simple huh? The first line loads your sound. The next line tells the sound to play.

Resources:

  1. cfxr – A sound tool for the Mac

WordPress – Custom Menu

Custom menus are a great options for WordPress. A menu is basically a list of links. This list of links can appear anywhere in your theme. You can customize the links that appear in the list via the admin panel.

A theme may contain as many custom menus as you care to add. Each can be customized through the admin panel. Custom menus can appear anywhere in your site you care to place them.

To get started create a function.php file in your theme folder.  Next add a <?php ?> tag to your functions.php file. Your functions.php should contain one <?php ?> tag and all of the custom php code should go between the <?php and ?>.

Now add the code to generate a new custom menu. Add the following inside the <?php and ?>:

// Notify WordPress that you want to run a function when the page initializes
add_action( 'init', 'register_my_menus' );

// Here's the function
function register_my_menus() {
   // Call this function to register a new menu
   // Declare a slug-name and a display name for the new menu
   register_nav_menus(
      array('header-menu' => __( 'Header Menu' ) )
   );
}

The first line tells WordPress to call on the function resgister_my_menus when the page initializes. The register_my_menus function is defined below. This functions calls on register_nav_menus, which is defined by WordPress. This function receives an Array describing the menu to create.The menu gets two names. A slug name which is used in code to refer to the menu. The other is a display name which is used when the name appears in a page.

The __() function used to wrap the display name, as in __(‘Header Menu’) is a special function used translate your page in to other languages. See the codex for more information. This function is not required but is good practice to include.

For more info refer to the codex: http://codex.wordpress.org/Function_Reference/wp_nav_menu

WordPress – functions.php

The functions.php file is a file that can be ioncluded in anything theme you might create for WordPress. This file is used to add unique functions to your WordPress theme. The functions.php file you can add new functionality to your site and modify the regular functions of WordPress. You can also gain access to many features of WordPress that are not available through the admin panel.

To use functions.php just create a plain text file named functions.php and add it to your theme folder. From here you will code that file that adds features to your theme.

Note: Every theme will have it’s own functions.php file and the features added by that file will only be in effect when that theme is active.

Corona – Variables

A variable is a storage space for a value that your program will use.Imagine you are creating a game. You might want to keep track of the game score. You would use a variable to do this. Often your programs will have many variables that refer to lots of different things.To assign a variable a value use the equal sign. For example:

score = 0

You can think of variables in the same way that you used x and y in algebra. These letters were placeholders for many possible values. In Lua, like many other programming languages variables get a name. The name can be any combination of characters. You can use variables as placeholders for values that will change as your program runs. For example you might display your score by setting the text property of a Text Object. For example:

scoreText = display.newText(...)
score = 0
-- later in your program
scoreText.text = score

Variables are called variables because the stored values can change. As your program runs you can change the value assigned to a variables. You can assign a new value at anytime to a variable using the equal sign.

The example above declares two variables scoreText and score. The first holds a reference to a new Text Object. The second holds a number, 0 in this case.

score = 10000

Note: Lua doesn’t support composite assignment operators like += and -=. Instead you’ll need to use:

score = score + 100

In Lua variables can be Global or Local. All variables are Global unless specifically declared as Local. Global variables can be accessed anywhere in the program. Local variables can only be accessed in the code block where they were declared. Declare a Local variable with the key word “local” the first time it appears. For example:

core = 100
local color = "red"

Here “score” is a global variable and color is a local variable.

Scope

Scope is a term that refers to where a variable can be accessed. The scope Global variables is easy to determine. Since they are accessible everywhere.

Local variables on the other hand take a little more thought. Unlike global variables, local variables have their scope limited to the block where they are declared. A block is the body of a control structure, the body of a function, or a chunk (the file or string with the code where the variable is declared).

x = 10             -- global variable
local i = 1        -- local to the chunk

while i<=x do
   local x = i*2   -- local to the while body
   print(x)        --> 2, 4, 6, 8, ...
   i = i + 1
end

if i > 20 then
   local x         -- local to the "then" body
   x = 20
   print(x + 2)
else
   print(x)        --> 10  (the global one)
end

print(x)           --> 10  (the global one)

It is good programming style to use local variables whenever possible. Local variables help you avoid cluttering the global environment with unnecessary names. Moreover, the access to local variables is faster than to global ones.

 

 

 

 

 

Corona – Physics

Corona has a nifty physics engine built in. The physics engine is used to simulate the interactions of objects using real world effects like gravity and motion. Elements, called “bodies”, can properties like density, friction and bounciness, which make them behave like real objects in the real world. Imagine you threw a ball. It might fly through the air while gravity pulled it toward the earth. Then it might bounce when it hit the ground and bounce again when it a wall. If you wanted to create a similar effect in an app the physics engine would help you do this and make it look realistic.

Using physics in Corona

Using physics in Corona is surprisingly easy and straight forward. The first step is to import the physics class and turn the physics engine on.

local physics = require("physics")
physics.start()

The first line import the physics engine and the second turns it on. The first line stores a reference to the physics engine in a variable named “physics”, we’ll use this to talk to the physics engine. Notice the second line uses the “physics” variable to turn the engine on. As we go you’ll see we’ll use the name “physics” to create physics “bodies” and set other options having to do with the physics environment.

Create a body

Next create a display object of some kind. This can be an image or a shape, then create a physics “body” from that object. The body is controlled by the physics environment and essentially takes over the display object moving it as it might move with the effects of gravity and bumping into other “bodies”.

Here’s a simple example that creates an image turns it into a physics body.

local box = display.newImage("box.png")
physics.addBody( box, "dynamic", { density=1.0, friction=0.3, bounce=0.65 } )

Here we call physics.addBody() with three parameters. The first, a display object to create the body from, box in this example. The second sets the type of object this will, “dynamic” in this case. “Dynamic” objects can move around and bump into other object. Last is a table containing options for the object, density, friction and bounce in this example.

  • density – the weight or mass of an object
  • friction – how much energy the object loses over time
  • bounce – the amount of energy the object retains when bounces off another object

Try it for yourself

Try this out for yourself. Follow the instructions below.

  1. Create a folder to hold your project.
  2. Create a new text file and save it into your folder with the name main.lua.
  3. Next add the following to your main.lua file
local physics = require("physics")
physics.setDrawMode( "debug" )
physics.start()

Here I imported the physics library and set some options. I set the draw mode to debug. This will display simple shapes for your physics objects. Second I started the physics engine running.

Now add a shape. Add a small image to your folder to use a physics object. Make this a square or a circle about 20px in diameter.

local ball = display.newImage("ball.png")
ball.x = 160
ball.y = 30
physics.addBody( ball, "dynamic",{ density=1.0, friction=0, bounce=0.65, radius=10 } )

Here you imported your image and made a display object from it, with the name “ball”. Then positioned the ball at the top center of the screen. Then you created a physics “body” from your display object. This is a “dynamic” object so it will move around. I added the “radius” property and set the value to the radius of the ball image. Since my ball was 20px I used 10 as the radius. Adding radius makes the “body” circular.

Test your project. At this point the ball should fall down the screen as if it were pulled by the force of gravity. The same thing would happen if you dropped a real ball. Of course a real ball would not fall forever it would hit the ground at some point.

Make a static body

Next we’ll create a ground plane.

local floor = display.newRect(0, 0, 320, 10)
floor.x = 160
floor.y = 475
floor:setFillColor( 255, 0, 0 )
physics.addBody( floor, "static", {density=1.0, friction=0.3 } )

Rather than using an image this time I created a rectangle shape object, 320px wide and 10px tall. The new shape is named “floor”.  Next we positioned the floor at the bottom center of the screen. Then give it a fill color of red. Last we create a new “body” from the floor shape. Here the type is “static”. Static shapes don’t move.

Test your project again. This time the ball falls and bounces off the ground. Change the bounce value assigned to the ball body, notice the change.

Gravity

Change the gravity in your physics world with physics.setGravity(x,y). Gravity is a constant force that pulls object in one direction. Add the following to your main.lua.

physics.setGravity(0,0)

Test your project again, notice the ball goes nowhere. This time there is no gravity. We can use this setting to simulate a top down view. Try out a few different values for the x and y of gravity. Set this back to 0 and 0 when you’re satisfied you understand what is happening with gravity.

Add more walls

Add three more walls to create a box. Add the following to your project file.

local ceiling = display.newRect(0, 0, 320, 10)
ceiling.x = 160
ceiling.y = 5
ceiling:setFillColor( 255, 0, 0 )
physics.addBody( ceiling, "static", {density=1.0, friction=0.3 } )

local left = display.newRect(0, 0, 10, 480)
left.x = 5
left.y = 240
left:setFillColor( 255, 0, 0 )
physics.addBody( left, "static", {density=1.0, friction=0.3 } )

local right = display.newRect(0, 0, 10, 480)
right.x = 315
right.y = 240
right:setFillColor( 255, 0, 0 )
physics.addBody( right, "static", {density=1.0, friction=0.3 } )

Here I’ve added three more boxes like the floor. These make up the left and right walls and the ceiling.

Test your project again. You should see the new walls and there should be a wall on each edge of the screen.

 

Velocity

Next we’ll make the ball move by tapping the screen. With each tap we’ll give the ball some linear velocity. That’s velocity in a straight line.

local function onTouch(event)
 local x = ball.x - event.x
 local y = ball.y - event.y
 ball:setLinearVelocity( x, y )
end

Runtime:addEventListener("touch", onTouch)

Here I created a function, onTouch. This function is triggered when the screen is touched. The event object passed to the function contains an x and a y position of where the touch occurred. The first to lines in the function the difference between the touch x and y and the ball x and y. The last line calls the setLinearVelocity method of ball and uses the two values to push the ball away from were you touch the screen.

The line below the function adds an event listener to the screen. This is what will trigger the event when a touch occurs.

Test your project again. This time the ball should sit still until you touch the screen. When you touch the screen the ball should move way from the point where the touch occurred bouncing off the walls as it moves.

Here’s a few things to try for yourself.

  1. Try different values for gravity
  2. Try out different values for density, friction and bounce for the ball
  3. Try removing the line physics.setDrawMode( “debug” )
  4. Try changing changing from debug to hybrid physics.setDrawMode( “hybrid” )