[SOLVED] Functions Performance

Hello Community, I have a question, what's better? Coding with functions inside another functions or create functions separately?

Examples:

Inside another function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
--Spawn Ball
function spawnBalls:tap ()
        ball = display.newImage ( "Ball.png" )
        ball:setReferencePoint ( display.CenterReferencePoint )
        ball.x = mRandom ( 40, wScreen - 40 )
        ball.y = mRandom ( 40, hScreen - 40 )
        physics.addBody ( ball, ballObject )
        ball.myName = "ball"
        ball:addEventListener ( "collision", ball )
        --Remove Ball Collision
        function ball:collision (e)
                if ( e.other.myName == "ball" ) then
                        e.target:removeSelf ()
                        e.target = nil
                        print ( "Removed Ball" )
                end
        end
        end
--Listener
Runtime:addEventListener ( "tap", spawnBalls )

We used our Corona® Profiler program to find out :o). We modified your app a bit so that it will do the exact same thing for control.

declared outside

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
local function collision(self, e)
                if ( e.other.myName == "ball" ) then
                        e.target:removeSelf ()
                        e.target = nil
                        print ( "Removed Ball" )
                end
        end
function spawnBalls:enterFrame ()
        ball = display.newImage ( "Icon.png" )
        ball:setReferencePoint ( display.CenterReferencePoint )
        ball.x = mRandom ( 40, _W - 40 )
        ball.y = mRandom ( 40, _H - 40 )
        physics.addBody ( ball, ballObject )
        ball.myName = "ball"
        ball:addEventListener ( "collision", ball )
        --Remove Ball Collision
        ball.collision = collision
        end
--Listener
Runtime:addEventListener ( "enterFrame", spawnBalls )

Wow! You have excellent tools guys... Thank you so much... Was useful...

views:1689 update:2011/11/26 9:01:35
corona forums © 2003-2011