Small bug in Collectible Items example

The Collectible Items example gave me errors on the console:"...attempt to call method 'removeSelf' (a nil value)" which was referring to the local onTransitionEnd handler inside of the onCollision event handler.

In the process, I learnt that the onComplete handler does have an event-kind of parameter, which is actually equal to the object itself...
The documentation about transition.to at "http://developer.anscamobile.com/reference/index/transitionto" is hopelessly incomplete (like much of Corona's doc...:-( ). Only when I introspected the parameter I found out it was the object itself. Guess Graham figured that out also somehow...

Anyway, what confused Lua was the fact that the local onTransitionEnd handler was defined with the same parameter name (event) as the outer-function onCollision event handler - theoretically I believe that the local inner "event" parameter should have precedence over the outer scoped one, but Lua doesn't seem to agree... Changing the onTransitionEnd parameter to event2 makes all work correctly.

The attached snippet of the onCollision handler corrects that little snafu.

It also changes the color of the text to something that shows up on my screen ;-)

Enjoy, Frank.

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
local function onCollision(self, event )
 
        if ( event.phase == "began" ) then
                if event.other.IsGround then
                        player.canJump = true
                        
                        if player.state == STATE_JUMPING then
                                player.state = STATE_IDLE
                        
                                player:prepare("anim" .. player.state)
                                player:play()
                        end
                elseif event.other.IsPickup then
                        
                        local item = event.other
                        
                        local onTransitionEnd = function(event2)
                                event2:removeSelf()
                        end
                                        
                        -- Fade out the item
                        transition.to(item, {time = 500, alpha = 0, onComplete=onTransitionEnd})
                        
                        local text = nil
                        
                        if item.pickupType == "score" then
                                
                                text = display.newText( item.scoreValue .. " Points!", 0, 0, "Helvetica", 50 )
                                
                        elseif item.pickupType == "health" then
                                
                                text = display.newText( item.healthValue .. " Extra Health!", 0, 0, "Helvetica", 50 )
                                
                        end
                        
                        if text then
                                text:setTextColor(100, 100, 100)
                                text.x = display.contentCenterX
                                text.y = text.height / 2
                                
                                transition.to(text, {time = 1000, alpha = 0, onComplete=onTransitionEnd})
                        end
                end
        elseif ( event.phase == "ended" ) then
                if event.other.IsGround then
                        player.canJump = false
                end
        end
end

Whoops, I foolishly assumed that Lua would be fine with the second variable also being called event. I will put in the fixes you suggested. Thanks again!

views:13140 update:2011/10/13 16:30:09
corona forums © 2003-2011