calling functions in storyboard.api

I'm using the storyboard, and when Im in the enterScene part to call functions it is not calling.
The function is at line 81(level1Presents()). I thought it would call itself, and I tried calling it elsewhere. to no avail... The program will run right up to that point and print '1:enterScene event'

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
----------------------------------------------------------------------------------
--
-- scenetemplate.lua
--
----------------------------------------------------------------------------------
 
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local p = 0
local bg1, floor1, santa, triangleShape, lever, present1, present2, present3, dropButton
 
local phys1 = {friction = .7, bounce = .1, density = .3}
local h = display.contentHeight
local w = display.contentWidth
 
physics = require('physics')
physics.start()
physics.setGravity(0,9.3)--gravity 3-5 good
--physics.setDrawMode('hybrid')
 
 
 
----------------------------------------------------------------------------------
-- 
--      NOTE:
--      
--      Code outside of listener functions (below) will only be executed once,
--      unless storyboard.removeScene() is called.
-- 
---------------------------------------------------------------------------------
 
---------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
---------------------------------------------------------------------------------
 
-- Called when the scene's view does not exist:
function scene:createScene( event )
        local group = self.view
                
                bg1 = display.newImage('bg1.jpg.')
                group:insert(bg1)
                
                floor1 = display.newRect(0, h-1, w, 1)
                floor1.name = 'floor1'
                physics.addBody(floor1, "static",{density = 9, friction = .7, bounce = .2})
                --floor1:addEventListener('collision', onCollision)
                group:insert(floor1)
                
                santa = display.newImageRect('santa.jpg', 100, 100)
                santa.x = w/2
                santa.y = h-40
                triangleShape = { 0,-50, 50,50, -50,50 }
                physics.addBody( santa, {density=9, friction=0.5, bounce=0, shape=triangleShape } )
                group:insert(santa, triangleShape)
        
                lever = display.newRect(w/2-350, h-125, 700, 30)
                lever:setFillColor(0, 230, 29)
                lever.name = 'lever'
                physics.addBody(lever,'dynamic', {density = 3, friction = .7, bounce = 0})
                myJoint = physics.newJoint('pivot', santa, lever, w/2, h-140)
                group:insert(lever, myJoint)
                
                
                -----------------------------------------------------------------------------
                
        --      CREATE display objects and add them to 'group' here.
        --      Example use-case: Restore 'group' from previously saved state.
        
        -----------------------------------------------------------------------------
        
end
 
 
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    --local group = self.view
    print( "1: enterScene event" )
        --level1Presents()
        
        local function level1Presents()
                p = p+1
                print (p)
                if (p ==1) then
                        present1 = display.newImageRect('present1.jpg', 50, 50)
                        physics.addBody(present1, 'kinematic', phys1)
                        present1.x = w/2
                        present1.y = h/7
                        present1:addEventListener('touch', onTouch)
                
                elseif (p == 2) then
 
                        present2 = display.newImageRect('present2.gif',50, 50)
                        physics.addBody(present2, 'kinematic', phys1)
                        present2.x = w/2 
                        present2.y = h/6
                        present2:addEventListener('touch', onTouch)
        
                elseif (p == 3) then
                
                        present3 = display.newImageRect('present3.gif', 50, 50)
                        physics.addBody(present3, 'kinematic', phys1)
                        present3.x = w/2 
                        present3.y = h/6
                        present3.rotation = 5
                        present3:addEventListener('touch', onTouch)
                end     
 
        end
end

Where do you ever call level1Presents()

When the enterScene event fires, it prints your string and then has nothing else to do. You define a function in it, but you never call it.

Yes you are exactly correct. I define it but I cannot call it. Notice line 79 I commented out because I tried calling it there and it gave me error.

Where do I call it?

Try this:

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
-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )
    --local group = self.view
    print( "1: enterScene event" )
        
        local function level1Presents()
                p = p+1
                print (p)
                if (p ==1) then
                        present1 = display.newImageRect('present1.jpg', 50, 50)
                        physics.addBody(present1, 'kinematic', phys1)
                        present1.x = w/2
                        present1.y = h/7
                        present1:addEventListener('touch', onTouch)
                
                elseif (p == 2) then
 
                        present2 = display.newImageRect('present2.gif',50, 50)
                        physics.addBody(present2, 'kinematic', phys1)
                        present2.x = w/2 
                        present2.y = h/6
                        present2:addEventListener('touch', onTouch)
        
                elseif (p == 3) then
                
                        present3 = display.newImageRect('present3.gif', 50, 50)
                        physics.addBody(present3, 'kinematic', phys1)
                        present3.x = w/2 
                        present3.y = h/6
                        present3.rotation = 5
                        present3:addEventListener('touch', onTouch)
                end     
 
        end
 
        level1Presents()
 
end

Awesome thank you!

Also can I forward reference all of my functions in the create scene so I don't have to worry about order?

I see many programmers setting up their functions

local function = {}

at the beginning than calling them later?

Generally its best practice to declare the functions at the top of the program and then call them later when you need them. Your "level1Presents" function could have been declared above the createScene function.

If you can do this, then you don't have to worry about forward declarations.

In many 2-pass compiled languages like C and C++ we don't have to worry about forward declaration very much so some programmers believe that functions should be defined near where you are using them. This philosophy tries to rear its head in Lua because its kind of the new way of doing things.

There will be times in Lua where you want to declare a function or variable near where you want to use it. The variable names have to exist before you first reference them, then you may need to declare the variable name first. That is the variable = function() syntax that you see:

1
2
3
4
5
6
7
8
9
10
11
12
13
local myFunction  -- pre-define the variable name
 
...
 
myFunction() 
 
...
 
myFunction = function()
    ...
    -- do stuff
    ...
end

Thanks I'm forward declaring all my variables and functions from now on. There doesn't seem to be anything 'bad' that can come from it. Is there?

One question, you forward declared your function as a variable. I've been studying some code where the author forward declared his functions as a table. Will their be differences in the outcome when using one or the other?

Interesting, I forward declared 'level1Presents' as both a variable and as a table(separate attempts) before 'createScene' and then tried calling it before defining it like I had done previously.(in the enterScene)

It should have worked but it didn't. Simulator says
'Attempt to call upvalue 'level1Presents' '

So no worries, I will go with calling it after defining and it works perfectly. Any ideas why that didn't work?

Personally I would have coded it calling it afterwards. I don't like the forward declaration method myself.

views:1826 update:2011/12/31 9:35:10
corona forums © 2003-2011