refresher on cross calling a function

I have a very simple problem that I am not understanding. In this mini game I just want to be able to touch touch 8 bees and they all play the same animations. The problem I am having is that bee is local and it needs to be global or needs to be crossed called.. I viewed the scopes for functions tutorial but I am not sure if I am going about it right.. Can someone point me in the right direction?

Here is my 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
display.setStatusBar( display.HiddenStatusBar ) -- HIDE STATUS BAR
  
local i = 1
 
local loqsprite = require('loq_sprite')
 
local screenW = display.contentWidth
local screenH = display.contentHeight
 
local BG = display.newImageRect("background.png", 1024, 768)
BG.x = screenW/2;  BG.y = screenH/2 
 
local bee = nil
 
local t1 = {
        
        {x=180,y=300},
        {x=280,y=300},
        {x=380,y=300},
        {x=480,y=300},
        {x=580,y=300},
        {x=680,y=300},
        {x=780,y=300},
        {x=880,y=300}
 
}
 
local function spawnBees ()
        for i = 1,8 do
                local bfactory = loqsprite.newFactory("bee")
            bee = bfactory:newSpriteGroup()
                bee.x = t1[i].x;  bee.y = t1[i].y
                bee:prepare("aniBee Bee")
        end
end
 
 
listener = function( event )
 
        local phase = event.phase
        
        if ( phase == "ended" ) then
                
        bee:play("aniBee Bee")
                                
        end
end
 
bee:addEventListener( "touch", listener ) 

Thanks @GrahamRanson

I got the bees to show up but the touch event doesn't work.. :(

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
display.setStatusBar( display.HiddenStatusBar ) -- HIDE STATUS BAR
  
local i = 1
 
local loqsprite = require('loq_sprite')
 
local screenW = display.contentWidth
local screenH = display.contentHeight
 
local BG = display.newImageRect("background.png", 1024, 768)
BG.x = screenW/2;  BG.y = screenH/2 
 
 
local t1 = {
        
        {x=180,y=300},
        {x=280,y=300},
        {x=380,y=300},
        {x=480,y=300},
        {x=580,y=300},
        {x=680,y=300},
        {x=780,y=300},
        {x=880,y=300}
 
}
 
 
local function spawnBees ()
        local bee = nil
        for i = 1,8 do
                local bfactory = loqsprite.newFactory("bee")
            bee = bfactory:newSpriteGroup()
                bee.x = t1[i].x;  bee.y = t1[i].y
                bee:prepare("aniBee Bee")
        end
end
 
 
local function onTouch(event)
        if "ended" == event.phase then       
        --bee:play("aniBee Bee")
           print("touched bee")                     
        end
 
        bee:addEventListener("touch",onTouch)
        return bee
end
 
 
local xTemp = spawnBees ()

You're adding the touch event listener within the touch event handler :-)

I don't know what you ment by that GrahamRanson ?:)

Is this what you mean.. If it is it, doesn't do any thing either...

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
display.setStatusBar( display.HiddenStatusBar ) -- HIDE STATUS BAR
  
local i = 1
 
local loqsprite = require('loq_sprite')
 
local screenW = display.contentWidth
local screenH = display.contentHeight
 
local BG = display.newImageRect("background.png", 1024, 768)
BG.x = screenW/2;  BG.y = screenH/2 
 
 
local t1 = {
        
        {x=180,y=300},
        {x=280,y=300},
        {x=380,y=300},
        {x=480,y=300},
        {x=580,y=300},
        {x=680,y=300},
        {x=780,y=300},
        {x=880,y=300}
 
}
 
local function onTouch(event)
        if "ended" == event.phase then       
        --bee:play("aniBee Bee")
           print("touched bee")                     
        end
end
 
local function spawnBees ()
        local bee = nil
        for i = 1,8 do
                local bfactory = loqsprite.newFactory("bee")
            bee = bfactory:newSpriteGroup()
                bee.x = t1[i].x;  bee.y = t1[i].y
                bee:prepare("aniBee Bee")
        end
        bee:addEventListener("touch",onTouch)
        return bee
end
 
local xTemp = spawnBees ()

In your spawnBees function you only add the event listener to the last bee created as it is added outside of the loop, give this a try:

1
2
3
4
5
6
7
8
9
10
11
12
local function spawnBees ()
        local bee = nil
        for i = 1,8 do
                local bfactory = loqsprite.newFactory("bee")
            bee = bfactory:newSpriteGroup()
                bee.x = t1[i].x;  bee.y = t1[i].y
                bee:prepare("aniBee Bee")
                 bee:addEventListener("touch",onTouch)
        end
       
        return bee
end

Cool thanks GrahamRanson!! That makes a lot more sense now!! Thanks a bunch!

I SEE THE LIGHT!

No problem, glad it helped.

it helped all right!

There are other scope problems....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
local function spawnBees ()
        local bee = nil
        for i = 1,8 do
                local bfactory = loqsprite.newFactory("bee")
            bee = bfactory:newSpriteGroup()
                bee.x = t1[i].x;  bee.y = t1[i].y
                bee:prepare("aniBee Bee")
        end
end
 
 
local function onTouch(event)
        if "ended" == event.phase then       
        --bee:play("aniBee Bee")
           print("touched bee")                     
        end
 
        bee:addEventListener("touch",onTouch)
        return bee
end
views:1469 update:2011/10/19 8:59:29
corona forums © 2003-2011