inoperative removeSelf

removeSelf works in one spot, but in another it says it's a nil value. Here's my code, mostly mooched from the forums:

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
local physics = require("physics");
physics.start();
system.activate( "multitouch" )
---
physics.setDrawMode("hybrid")
---
display.setStatusBar( display.HiddenStatusBar )
--
_H = display.contentHeight
_W = display.contentWidth
--
print (_H .. ", " .. _W)
--
local worldLimits = { XMin=0 , YMin=0 , XMax=_W , YMax=_H }
 
 
 
local hand1 = display.newCircle (0, 0, 25)
hand1:setFillColor(255, 255, 255)
hand1.x = _W * 0.5
hand1.y = _H * 0.5
--
physics.addBody(hand1, {density=50, radius=20});
 
 
--Kill Joint
 
                function destroy( event )
                        --if (hand1.weld ~= nil) then
                                                                --print (hand1.weld)
                                hand1.weld:removeSelf()
                        --end
                end
 
 function dragBody( event )
    if (event.phase == "began") then
                if (hand1.weld ~= nil) then
                        timer.performWithDelay( 0, destroy, 1 )
                end
        -- set drag focus to the ball to be dragged
        display.getCurrentStage():setFocus( event.target )
        event.target.touch = physics.newJoint( "touch", event.target, event.x, event.y )
                --get rid of the joint
    elseif (event.phase == "moved") then
        -- drag the ball
        event.target.touch:setTarget( event.x, event.y )
    else
        -- stop dragging the ball
        display.getCurrentStage():setFocus( nil )
        event.target.touch:removeSelf()
        end
                        
        -- tell system we've handled the touch
        return true
end
 
---[[
--Make joint
                                
                                --...because that can't be done in the collision event handler
                function create( event )
                        -- only attach the joint to balls and weld is not already attached (use a table to add multiple joints)
                        if (hand1.weld == nil and hand1.other ~= nil) then
                                -- add joint
                                hand1.weld = physics.newJoint( "weld", hand1, hand1.other, hand1.x, hand1.y )
                                -- we don't need to keep track of the other object to be jointed
                                hand1.other = nil
                        end
                end
                
--Collision
                                
                -- when collisions happen start the timer to add the joint because it can't be done here
                function hand1:collision( event )
                        -- only attach the joint to balls, not walls!
                        if (event.other.type == "wall") then
                                -- keep track of the other object to join to
                                hand1.other = event.other
                                -- start the timer on a very short expiry
                                timer.performWithDelay( 0, create, 1 )
                        end
                end
 
                                
                                
                                
                        --]]    
hand1:addEventListener( "touch", dragBody )
hand1:addEventListener( "collision", hand1 )
 
 
 
camera = display.newGroup()
 
 
local function makePlatform (name, x, y, width, height)
        local name = display.newRect (0, 0, width, height)
        name:setFillColor(255, 246, 0)
        name.x = x
        name.y = y
        physics.addBody(name, "static", {bounce = .4, friction=5})
        camera:insert(name)
        name.type = "wall"
end
--
makePlatform (wall1, 0, worldLimits.YMax * 0.5, 5, worldLimits.YMax)                --left wall
makePlatform (wall2, worldLimits.XMax, worldLimits.YMax * 0.5, 5, worldLimits.YMax) --right wall
makePlatform (ground, worldLimits.XMax * 0.5, worldLimits.YMax, worldLimits.XMax, 5)--bottom
makePlatform (ceiling, worldLimits.XMax * 0.5, 0, worldLimits.XMax, 5)              --top
views:1500 update:2011/10/17 8:58:49
corona forums © 2003-2011