×
A new build of Corona SDK is now available to subscribers. Not a subscriber? Subscribe now.
CoronaSDK 2012.741 | Released: 7 Feb 2012, 8:45am | What's New | Download Now

Frame Animation 2

Features demonstrated:

How to use table listeners for event handling, for more of an object-oriented approach. This is an example of one convenient way to structure and organize code.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local function newBall( params )
        local xpos = display.contentWidth*0.5
        local ypos = display.contentHeight*0.5
        local circle = display.newCircle( xpos, ypos, params.radius );
        circle:setFillColor( params.r, params.g, params.b, 255 );
        circle.xdir = params.xdir
        circle.ydir = params.ydir
        circle.xspeed = params.xspeed
        circle.yspeed = params.yspeed
        circle.radius = params.radius
 
        return circle
end
 
local params = {
        { radius=20, xdir=1, ydir=1, xspeed=2.8, yspeed=6.1, r=255, g=0, b=0 },
        { radius=12, xdir=1, ydir=1, xspeed=3.8, yspeed=4.2, r=255, g=255, b=0 },
        { radius=15, xdir=1, ydir=-1, xspeed=5.8, yspeed=5.5, r=255, g=0, b=255 },
--      newBall{ radius=10, xdir=-1, ydir=1, xspeed=3.8, yspeed=1.2 }
}
 
local collection = {}
 
-- Iterate through params array and add new balls into an array
for _,item in ipairs( params ) do
        local ball = newBall( item )
        collection[ #collection + 1 ] = ball
end
 
-- Get current edges of visible screen (accounting for the areas cropped by "zoomEven" scaling mode in config.lua)
local screenTop = display.screenOriginY
local screenBottom = display.viewableContentHeight + display.screenOriginY
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX
 
function collection:enterFrame( event )
        for _,ball in ipairs( collection ) do
                local dx = ( ball.xspeed * ball.xdir );
                local dy = ( ball.yspeed * ball.ydir );
                local xNew, yNew = ball.x + dx, ball.y + dy
 
                local radius = ball.radius
                if ( xNew > screenRight - radius or xNew < screenLeft + radius ) then
                        ball.xdir = -ball.xdir
                end
                if ( yNew > screenBottom - radius or yNew < screenTop + radius ) then
                        ball.ydir = -ball.ydir
                end
 
                ball:translate( dx, dy )
        end
end
 
Runtime:addEventListener( "enterFrame", collection );