Developing iPhone/iPad/Android Games with Corona: Creating Animations

Posted by Dr. Burton on July 8, 2010 in Android, Corona, iPad, iPhone, Mobile |

In this demonstration, we will take our existing Hello World project and add some animation.

To get started, we have the existing code:

local textObj = display.newText(“Hello World!”, 50, 50, nil, 24)
textObj:setTextColor( 255, 255, 255)

local mybutton = display.newImage( “button.png” )
mybutton.x = display.contentWidth/2
mybutton.y = display.contentHeight-75

local h = textObj.height
local w = textObj.width

function mybutton:tap( event )
textObj.x = math.random(w/2, display.contentWidth – (w/2))
textObj.y = math.random(h/2, display.contentHeight -(100 + h/2))

mybutton:addEventListener( “tap”, mybutton )

This little Corona app will randomly place the text “Hello World” within the confines of the screen canvas area.

Today we are going to add the ability for the text to smoothly travel from current location to its new location that has been randomly generated.
If you are familiar with Flash, then you have probably used tweening.  In Corona, we can create similar transitions using the transition.to command

transition.to( object, {array} )

Within the array of the transition.to parameters, we pass what we would like to change and how quickly we want that transition to occur.

My new function (replacing the old mybutton fuction) looks like:
function mybutton:tap( event )
transition.to(textObj, { time=1000, x = math.random(w/2, display.contentWidth – (w/2)),
y = math.random(h/2, display.contentHeight -(100 + h/2)) } )

In this function, I have passed the textObj object as the object.  In the array, I’m passing it a time parameter that is set to 1000 milliseconds (or 1 second), and then the x and y variables.  As you can see in the code, this is exactly the same code that we had previously, it is just set to transition the change of location over 1 second.

You can see the full demonstration here:

[tubepress video=cm_ys-M3Mng]

Note: I have changed display.stageWidth and display.stageWidth to display.contentWidth & display.contentHeight for version 2.0 of Corona.  As of 2.0 stageHeight & stageWidth have been depreciated.

Tags: , , , , , , , ,


  • Digital Encounters: Daniel Suarez, Call of Duty: World at War | Game guide says:

    […] Developing iPhone/iPad/Android Games with Corona: Creating … […]

  • Braxton Gregory Smith says:

    Hey I was wondering if you could help me with this error I am getting which states:

    main.lua:1: in main chunk
    Runtime error: bad argument #2 to ‘_newText’ (number expected, got nil)
    stack traceback:
    [C]: ?
    [C]: in function ‘_newText’
    ?: in function ‘newText’

    My code for the application is:

    local textObj = display.newText( “Hello World!”, x, y, native.systemFont, 24 )
    textObj:setTextColor( 255, 255, 255 )

    local myButton = display.newImage( “button.png” )
    myButton.x = display.contentWidth/2.0
    myButton.y = display.contentHeight – 75

    function myButton:tap( event )
    local w = textObj.width / 2
    local h = textObj.height / 2
    local tempT = 1000
    local tempX = math.random( h, display.contentHeight – h )
    local tempY = math.random( w, display.contentWidth – w )
    local params = { time = tempT, x = tempX, y = tempY }
    transition.to( textObj, params )

    myButton:addEventListener( “tap”, myButton )

  • Braxton Gregory Smith says:

    figured it out. stupid mistake

  • me says:

    typical, when you figure something out please do shre it with the rest of the world “ok, it was stupid mistake” wont help others

  • Dr. Burton says:

    Unless it is just a typo. Hey people, let’s try to be friendly and supportive of each other!

  • Edmond says:

    Dr. Burton is the man!

  • atul says:

    I want to add Eventlistener to buttons in an array defined as
    mybutton[1] = display.newImage(‘1.png’)
    mybutton[2] = display.newImage(‘2.png’)

    I am using the code
    for i =1,2 do
    local button = mybutton[i].graphics;
    button:addEventListener(“tap”, OnTouch)

    It shows a run-time error “attempt to index local button’ ,a nil value

    What is the error and how to I add the Eventlistener to an array of buttons, I have almost 15 buttons?


  • Dr. Burton says:

    I think you might be better off using dynamic function calls for each of the buttons:
    local button1 = function( event )

  • praveen says:

    How should i want to use sprite in Director class

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Copyright © 2010-2022 Burtons Media Group All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.