I would expect this code to cause the object e.other to fade and then be removed. But when I run it, all that is visible is e.other being removed. I have increased the transition, but still not delay. Without the losecenter function, the delay works. What am I doing wrong?
thanks in advance
function losecenter(item)
item:removeSelf()
end
function onCollision(self,e)
if e.phase=="began" then
x=e.other.x
y=e.other.y
if string.sub(e.other.name,1,6)=="center" and self.name=="boulder" then
e.other.bodyType="dynamic"
transition.to(e.other, { time=1000,alpha=.0,onComplete=losecenter(e.other)})
--the rest of the code doesn't really matter, e.other should have disappeared after if faded.
text=display.newText("+100",x,y,native.sysemfont,20)
text:setTextColor(255,255,255)
score=score+100
camera:insert(text)
transition.to( text, { time=1500, alpha=.2, x=(x-50), y=(y-50) } )
scoretext.text=score
end
end
end
Try rearranging like this and not pass any parameters in the onComplete?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| function onCollision( self, e )
if e.phase == "began" then
local item = e.other
x = item.x
y = item.y
if string.sub( item.name, 1, 6 ) == "center" and self.name == "boulder" then
item.bodyType = "dynamic"
local function losecenter()
item:removeSelf()
item = nil
end
transition.to( item, { time = 1000, alpha = 0, onComplete = losecenter })
--the rest of the code doesn't really matter, e.other should have disappeared after if faded.
text = display.newText("+100", x, y, native.systemfont, 20 )
text:setTextColor( 255, 255, 255 )
score = score + 100
camera:insert( text )
transition.to( text, { time = 1500, alpha = 0.2, x = ( x - 50 ), y = (y - 50 )})
scoretext.text = score
end
end
end |
Thanks, will give it a try and let you know if it flies.
Your fix worked perfectly. Then to try to gain some understanding I tested substituting "e.other" for "item" and it also works. That means the solution comes down to having moved the losecenter function inside of the onCollision function.
I tested with the losecenter function outside of the onCollision function and sure enough, it doesn't work -- doesn't remove the object. Go figure....
Not quite sure I can develop a logical understanding of why it works...but thanks for giving me a solution!!!